【发布时间】:2016-09-19 11:12:17
【问题描述】:
我目前在同一个 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
您可能会看到,let updateTask 正在尝试获取indexPath.row,但这是不可能的,因为我不能在numberOfRowsInSection 中有两个返回值,这是一个问题,因为该数字指的是数据存储在我的 firebase 数据库中的位置。如何修改它以使其正常工作?
希望你们明白我的意思,否则请告诉我,我会尽力解释得更好:-)
【问题讨论】:
-
请添加
cellForRowAtIndexPath方法的其余部分,因为这在这种情况下也很重要。 -
请告诉我您希望以什么顺序显示两个不同的单元格?所有单元格后跟所有任务单元格?
-
@SamM 这就是我目前所有的
cellForRowAtIndexPath,如果我让它工作,以后还会有更多,我想在 cellTask 之前显示单元格 - 如果可能的话? -
检查我的答案,它将显示所有单元格,然后显示所有单元格任务。如果您需要它们像 cell/cellTask/cell/cellTask... 告诉我,我会更新答案。
-
我宁愿拥有那个!那将是真棒! @萨姆
标签: ios swift uitableview firebase