D. *_*nna 0 uitableview ios firebase swift
我目前在同一uitableview上显示两种不同类型的自定义单元格时遇到问题。
到目前为止,我管理的工作是接收对更新单元格的“更新” cell。我只是无法弄清楚如何也让numberOfRowsInSection返回两个值,所以我的两个单元格都将显示。
让我解释一下我的代码:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return updates.count
return updatesTask.count // I CANNOT DO THIS - what can I do instead?
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell
let update = updates[indexPath.row]
let updateTask = updatesTask[indexPath.row]
// Example of the two different cells that need different data from firebase
cell.nameLabel.text = update.addedByUser
cellTask.nameLabel.text = updateTask.addedByUser
Run Code Online (Sandbox Code Playgroud)
正如您可能会看到的那样,let updateTask试图获取一个,indexPath.row但是那是不可能的,因为我在中不能有两个返回值numberOfRowsInSection,这是一个问题,因为该数字是指数据存储在我的Firebase数据库中的位置。 。我该如何修改使其起作用?
希望你们明白我要怎么做,否则让我知道,我会尽力更好地解释:-)
如果您想将它们分成两部分,@Callam的答案很好。
如果您希望所有人都在同一部分,这就是解决方案。
首先,在numberOfRowsInSection方法中,您需要返回这两个数组计数之和,如下所示:return (updates.count + updatesTask.count)
然后,您需要配置如下cellForRowAtIndexPath方法:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row < updates.count{
// Updates
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
let update = updates[indexPath.row]
cell.nameLabel.text = update.addedByUser
return cell
} else {
// UpdatesTask
let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell
let updateTask = updatesTask[indexPath.row-updates.count]
cellTask.nameLabel.text = updateTask.addedByUser
return cellTask
}
}
Run Code Online (Sandbox Code Playgroud)
这将显示所有单元格,然后显示所有cellTasks。
如果updatesarray和updatesTaskarray的项目数相等,并且您想要一一显示它们,则可以使用以下命令:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row % 2 == 0 {
// Updates
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
let update = updates[indexPath.row/2]
cell.nameLabel.text = update.addedByUser
return cell
} else {
// UpdatesTask
let cellTask:tasksTableViewCell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! tasksTableViewCell
let updateTask = updatesTask[indexPath.row/2]
cellTask.nameLabel.text = updateTask.addedByUser
return cellTask
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12032 次 |
| 最近记录: |