ios - 如何让两个不同的表格 View 单元格始终成对显示在同一个表格 View 中?

标签 ios swift uitableview tableview

我想把两个不同的 tableViewCells 放在同一个 tableView 中,这样它们总是成对出现。第一个 tableViewCell 应该显示可由用户调整的开始和结束时间。 (可能是 datePicker)第二个 tableViewCell 应该显示一个也可编辑的 textFieldtableViewCells 都应该有一个可以选中或不选中的小按钮。我想将该数据(时间、文本、按钮状态)存储在某处。

我为两个单元格设置了一个结构作为 dataType 并创建了一个数组。我还设置了 cellForRowAt,但在运行应用程序时出现错误。

创建数据类型:

struct ScheduleCell{

    var id: Int
    var buttonState: Bool
    var text: String
    var time01: String
    var time02: String

}

存放数据的地方:

var scheduleCellEntries: [ScheduleCell]! = [ScheduleCell(id: 2, buttonState: false, text: "Test", time01: "12:30", time02: "12:20"), ScheduleCell(id: 2, buttonState: false, text: "Test", time01: "12:30", time02: "12:20"), ScheduleCell(id: 2, buttonState: false, text: "Test", time01: "12:30", time02: "12:20"), ScheduleCell(id: 2, buttonState: false, text: "Test", time01: "12:30", time02: "12:20")]

分配行数:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        var count: Int?

        if tableView == scheduleTableView{
            count = scheduleCellEntries.count * 2

        }
    return count!
}

将数据提取到单元格中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if tableView == scheduleTableView {

            if indexPath.row % 2 != 0{
                let cell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath) as! scheduleTableViewCell
                cell.scheduleTextField.text = scheduleCellEntries[indexPath.row].text


        return cell
}else{
        let cell = tableView.dequeueReusableCell(withIdentifier: "scheduleDateCell", for: indexPath) as! ScheduleDateTableViewCell


        return cell
}

当我运行该应用程序时,出现以下错误 Thread 1: Fatal error: Index out of range!

最佳答案

问题是您在 scheduleCellEntries 中有 X 条记录,但自从您返回 scheduleCellEntries.count * 2 以来,表中的单元格数量是原来的两倍。在 cellForRowAt 中,您应该使用 indexPath.row/2 而不是使用 indexPath.row 来访问 scheduleCellEntries 中的数据,因此当表格要求第 0 行和第 1 行的数据时,会选择相同的数据。

我稍微更改了您的示例代码,我添加了一个具有适当索引的变量来获取您的数据 let indexOnData = indexPath.row/2 并在 scheduleCellEntries 上使用它.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == scheduleTableView {
        let indexOnData = indexPath.row / 2
        if indexPath.row % 2 != 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "scheduleCell", for: indexPath) as! scheduleTableViewCell
            cell.scheduleTextField.text = scheduleCellEntries[indexOnData].text
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "scheduleDateCell", for: indexPath) as! ScheduleDateTableViewCell

        }
        return cell
    }
}

关于ios - 如何让两个不同的表格 View 单元格始终成对显示在同一个表格 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56031315/

相关文章:

ios - UICollectionView Header - 计算复杂 View 的大小

ios - 如何在后台更新 UILabel 计时器

ios - 发送命令到斑马打印机xcode

iphone - UITableViewCell 中的 VoiceOver 用户交互

swift - 我可以在运行时根据条件数组在 Couchbase Lite swift 中创建 Where 子句吗?

Swift:添加方向按钮以固定注释

swift - 尝试在 TableView 中显示图像时出错

ios - swift 4 : Fatal error: Unexpectedly found nil while unwrapping an Optional value

iOS - 带集群的 Mapbox,MGLSymbolStyleLayer 不显示自定义图像

swift - SKEmitterNode iOS 8 vs iOS 9 如何获得相同的结果?