ios - 在 TableView 中返回多个 dequeueReusableCells,Swift

标签 ios swift uitableview

我试图解决的问题是为加载的任务返回多个单元格,因为如果不加载全新的任务,我就无法迭代。

每个 View 都有一个或多个单元格,应该为该特定任务加载,但我似乎无法解决该问题。感谢您的帮助!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let categoryCells = self.tasks[indexPath.row] as? Task
    var cellChooser: BoredTaskCell!

    if selectedTaskCategory == "Movie"{
    let cell = tableView.dequeueReusableCellWithIdentifier("imdbCell") as! BoredTaskCell!
        cell!.selectionStyle = UITableViewCellSelectionStyle.None
        cellChooser = cell
    }
    else if selectedTaskCategory == "Lifestyle" {
        let emailCell = tableView.dequeueReusableCellWithIdentifier("emailCreatorCell") as! BoredTaskCell!
        emailCell!.selectionStyle = UITableViewCellSelectionStyle.None
        cellChooser = emailCell

    }
    else {
        let emptyCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! BoredTaskCell!
        cellChooser = emptyCell
        emptyCell!.selectionStyle = UITableViewCellSelectionStyle.None
    }

    return cellChooser
}

最佳答案

简短的回答是,您无法从一次调用 cellForRowAtIndexPath 中返回两个单元格。但要实现您想要的效果,请更改构建 tableView 的方式,以便它为每个任务分配一个部分,而不是为每个任务分配一个单元格。即使用 indexPath.section 作为数组的索引。您将需要修改所有 TableView 数据源方法(例如,numberOfSectionsInTableView 将返回self.tasks.count)。诀窍在于 numberOfRowsInSection,它通常应返回 1,但对于那些需要两个单元格的任务,它应返回 2。然后在 cellForRowAtIndexPath 中,您确定要执行哪个任务使用 indexPath.section 显示。然后,如果indexPath.row为0,则使用一种单元格类型(例如imdbcell),如果为1,则使用另一种单元格类型(例如消息单元格)。

关于ios - 在 TableView 中返回多个 dequeueReusableCells,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31365876/

相关文章:

swift - 在didSelectRowAt中调用moveRowAt方法?

iphone - 如何从uitableviewcell中的uibutton加载弹出窗口

ios - 以编程方式将 UIImageView 添加到 UITableViewCell

ios - Cordova 振动插件在 ios 中不起作用

ios - 从一个应用程序(IOS)控制手机中的其他应用程序

ios - 为什么 UICollectionViewCell 的导出为零?

ios - 向 UITableViewCell 添加隐藏信息

ipad - 嵌入在 UINavigationBar/Item 中的 UISegmentedControl

ios - 如何为 native iOS 应用程序创建测试用户?

ios - Swift:在 UITableViewCell 中异步加载图像 - 自动布局问题