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