Ios 在单个视图中控制多个表视图
我在集合视图中嵌套了一个表视图,我将返回3个(将来可能会更多)集合视图单元格,我想知道是否可以在每个集合单元格中显示不同的内容?我附上了一些截图,以便更好地了解我正在拍摄的内容。谢谢Ios 在单个视图中控制多个表视图,ios,swift,uitableview,Ios,Swift,Uitableview,我在集合视图中嵌套了一个表视图,我将返回3个(将来可能会更多)集合视图单元格,我想知道是否可以在每个集合单元格中显示不同的内容?我附上了一些截图,以便更好地了解我正在拍摄的内容。谢谢 是的,你可以。您需要为您拥有的每个tableView设置一个属性,并在delegate方法中对其进行如下比较 class Some: UIViewController { var firstTableView: UITableView var secondTableView: UITableVie
是的,你可以。您需要为您拥有的每个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
}
}
是的,你可以。您需要为您拥有的每个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
}
}
可以将UITableViewDelegate/UITableViewDataSource方法与if-else条件或类似的条件一起使用 例如 但我认为,如果您对每个表使用单独的控制器类,那么就可以很容易地管理代码 但这取决于您拥有的数据类型。如果数据完全不相关,您可以使用3个不同的控制器。 或者,如果您可以在3个表中重用数据和代码,那么您可以决定是要使用3个不同的控制器,还是要使用上述方法的i类 例如
可以将UITableViewDelegate/UITableViewDataSource方法与if-else条件或类似的条件一起使用 例如 但我认为,如果您对每个表使用单独的控制器类,那么就可以很容易地管理代码 但这取决于您拥有的数据类型。如果数据完全不相关,您可以使用3个不同的控制器。 或者,如果您可以在3个表中重用数据和代码,那么您可以决定是要使用3个不同的控制器,还是要使用上述方法的i类 例如
使用
tag
区分您的表视图,并检查每个数据源方法和委托方法中的标记。使用tag
区分您的表视图,并检查每个数据源方法和委托方法中的标记。谢谢,我将尝试此方法。但是,如果我计划有两个以上的TableView,我可以继续添加其他语句吗?是的,你可以添加任何你想添加的TableView,我在过去几天尝试实施你的解决方案,但我遇到了一些问题。当我试图实现table view全局变量时,我得到一个编译器错误,说我的类没有初始值设定项,所以我首先尝试强制展开代码>像这样(var tableView1:UITableView!)(var tableview2:UITableView!)
但这给了我一个exc\u bad\u指令。因此,我尝试作为可选的?
,这消除了致命错误,但现在没有显示任何数据。委托和数据源设置为VC。你能帮忙吗?谢谢谢谢,我试试这个。但是,如果我计划有两个以上的TableView,我可以继续添加其他语句吗?是的,你可以添加任何你想添加的TableView,我在过去几天尝试实施你的解决方案,但我遇到了一些问题。当我试图实现table view全局变量时,我得到一个编译器错误,说我的类没有初始值设定项,所以我首先尝试强制展开代码>像这样(var tableView1:UITableView!)(var tableview2:UITableView!)
但这给了我一个exc\u bad\u指令。因此,我尝试作为可选的?
,这消除了致命错误,但现在没有显示任何数据。委托和数据源设置为VC。你能帮忙吗?谢谢
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == table1 { // table1 is a global var for the table
} else if tableView == table2 {
}
}
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