Во первых, есть ViewController, в нем находятся две tableView. Одна таблица принадлежит ViewController, а другая иному классу. Вопрос первый, можно ли сделать две tableView на одно viewController без использования других классов? Ибо у меня не вышло. И во вторых, исходя из того, что у меня две таблицы с разными классами, но находятся на одном ViewController, мне нужно изменить label при нажатии на ячейку второй таблицы другого класса, но он не видит label, так как он в другом классе. Можно ли как-то взаимодействовать с label, который в другом классе? Либо же как можно поместить две таблицы в один viewController?
1 ответ
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableView1: UITableView!
/// A simple data structure to populate the table view.
struct PreviewDetail {
let title: String
let preferredHeight: Double
}
let sampleData = [
PreviewDetail(title: "Small", preferredHeight: 160.0),
PreviewDetail(title: "Medium", preferredHeight: 320.0),
PreviewDetail(title: "Large", preferredHeight: 0.0) // 0.0 to get the default height.
]
let sampleData1 = [
PreviewDetail(title: "One", preferredHeight: 160.0),
PreviewDetail(title: "Two", preferredHeight: 320.0),
PreviewDetail(title: "Three", preferredHeight: 0.0), // 0.0 to get the default height.
PreviewDetail(title: "More", preferredHeight: 0.0) // 0.0 to get the default height.
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView1.dataSource = self
tableView1.delegate = self
tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of items in the sample data structure.
var count:Int?
if tableView == self.tableView {
count = sampleData.count
}
if tableView == self.tableView1 {
count = sampleData1.count
}
return count!
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell?
if tableView == self.tableView {
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let previewDetail = sampleData[indexPath.row]
cell!.textLabel!.text = previewDetail.title
}
if tableView == self.tableView1 {
cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath)
let previewDetail = sampleData1[indexPath.row]
cell!.textLabel!.text = previewDetail.title
}
return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("did select: \(indexPath.row) ")
}
}
-
спасибо, код немного подправить пришлось, а так все как нужно)) спасибо! – Vladislav Bublik 27 июл '18 в 8:32