Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使两个不同的表视图单元格始终成对显示在同一个表视图中?_Ios_Swift_Uitableview_Tableview - Fatal编程技术网

Ios 如何使两个不同的表视图单元格始终成对显示在同一个表视图中?

Ios 如何使两个不同的表视图单元格始终成对显示在同一个表视图中?,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,我想将两个不同的tableView单元格放在同一个tableView中,以便它们总是成对出现。第一个tableViewCell应显示用户可调整的开始和结束时间。(可能是datePicker)第二个tableViewCell应该显示一个textField,该字段也可以编辑。两个tableViewCells都应该有一个小按钮,可以选中也可以不选中。我想把数据(时间、文本、按钮状态)存储在某个地方 我为两个单元格设置了一个结构作为数据类型,并创建了一个数组。我还设置了cellForRowAt,但在运行

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

我为两个单元格设置了一个结构作为
数据类型
,并创建了一个数组。我还设置了
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
}

当我运行应用程序时,我得到以下错误
线程1:致命错误:索引超出范围

问题是您在
scheduleCellEntries
中有X个记录,但自从您返回
scheduleCellEntries.count*2
以来,表中的单元格数是原来的两倍。在
cellForRowAt
中,您应该使用
indexPath.row
来访问
scheduleCellEntries
中的数据,而不是使用
indexPath.row/2
,因此,当表请求第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
    }
}

问题是,您在
scheduleCellEntries
中有X个记录,但自从返回
scheduleCellEntries.count*2
以来,表中的单元格数是原来的两倍。在
cellForRowAt
中,您应该使用
indexPath.row
来访问
scheduleCellEntries
中的数据,而不是使用
indexPath.row/2
,因此,当表请求第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
    }
}

返回
count*2
作为行数是正确的,但在访问数组时需要将
除以2,否则您将尝试访问不存在的索引。就我个人而言,我会创建一个包含所有必需UI元素的单元格,而不是尝试创建两个单元格。创建一个包含需要显示的数据的ScheduleCell。因为numberOfRowsInSection无法猜测您正在尝试做什么。如果它与某个参考值匹配,您将显示它,然后您可以更改tableviewcell,为什么将数据拆分为2个单元格很重要?如果这些成对的单元总是在一起,我建议创建一个包含所有必要元素的单个单元。在这种情况下,您将使用numberOfRowsInSection=scheduleCellEntries.count返回
count*2
作为行数是正确的,但在访问数组时需要将
除以2,否则您将尝试访问不存在的索引。就我个人而言,我会创建一个包含所有必需UI元素的单元格,而不是尝试创建两个单元格。创建一个包含需要显示的数据的ScheduleCell。因为numberOfRowsInSection无法猜测您正在尝试做什么。如果它与某个参考值匹配,您将显示它,然后您可以更改tableviewcell,为什么将数据拆分为2个单元格很重要?如果这些成对的单元总是在一起,我建议创建一个包含所有必要元素的单个单元。在这种情况下,您将拥有numberOfRowsInSection=scheduleCellEntries.count