在单个视图中控制多个表视图
我有一个嵌套在集合视图中的表视图,并且我返回3(可能更多将来)集合视图单元格,并且我想知道是否可以在每个视图中呈现不同的内容其中一个收集单元?我附上了一些截图,以更好地理解我正在采取的行动。谢谢。在单个视图中控制多个表视图
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
cell.textLabel?.text = "Homeroom"
cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
cell.selectionStyle = .None
return cell
}
当然可以。你需要设置属性为每个的tableView你和delegate
方法就比较喜欢下面
class Some: UIViewController {
var firstTableView: UITableView
var secondTableView: UITableView
override func viewDidLoad() {
firstTableView = YOUR_FIRST
secondTableView = YOUR_Second
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if tableView == firstTableView {
return 2;
}
else if tableView == secondTableView {
return 1;
}
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if tableView == firstTableView {
return 2;
}
else if tableView == secondTableView {
return 1;
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
if tableView == firstTableView {
cell = tableView.dequeueReusableCellWithIdentifier("cellOfFirstTableView", forIndexPath: indexPath) as! UITableViewCell
}
else if tableView == secondTableView {
cell = tableView.dequeueReusableCellWithIdentifier("cellOfSecondTableView", forIndexPath: indexPath) as! UITableViewCell
}
// Configure the cell...
if tableView == firstTableView {
cell.textLabel?.text = "Homeroom"
cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
cell.selectionStyle = .None
}
else if tableView == secondTableView {
cell.textLabel?.text = "Homeroom"
cell.detailTextLabel?.text = "8:15 AM - 9:00 AM"
cell.selectionStyle = .None
}
return cell
}
}
谢谢我会试试这个。但是如果我打算有两个以上的桌面视图,我可以不断添加其他语句吗? – Hightower98
是的,你可以,你可以添加任何你想要的表格 – iSashok
我试着在过去几天实施你的解决方案,但我遇到了一些问题。当我试图实现表视图全局变量时,我得到一个编译器错误,说我的类没有初始化器,所以我首先尝试强制unwrap'!'就像这样('var tableView1:UITableView!)(var tableview2:UITableView!) '但那给了我一个exc_bad_instruction。所以我尝试作为一个可选的'?',并摆脱了致命的错误,但现在没有数据显示。委托和数据源设置为VC。你能帮忙吗?谢谢! – Hightower98
可以使用的UITableViewDelegate/UITableViewDataSource方法用的if else条件或某些事情类似
如。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == table1 { // table1 is a global var for the table
} else if tableView == table2 {
}
}
但我认为这将是非常清楚,如果你使用单独的控制器类为每个表,所以你可以轻松地管理代码。
但这取决于你有什么类型的数据。如果数据完全不相关,则可以使用3个不同的控制器。 或者,如果您可以在3个表格中重复使用数据和代码,那么您可以决定是否要使用3个不同的控制器,或者使用上述方法使用i类。
例如。
let table1Controller = Table1Controller(dataList1)
let table2Controller = Table2Controller(dataList2)
let table3Controller = Table3Controller(dataList3)
table1.delegate = table1Controller
table1.dataSource = table1Controller
table2.delegate = table2Controller
table2.dataSource = table2Controller
table3.delegate = table3Controller
table3.dataSource = table3Controller
在您的每个数据源方法和委托方法中使用'tag'和检查标记来区分您的tableviews – xmhafiz