<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
关于 UITableView
,我遇到了一些问题。简而言之,我有一个包含用户输入数字的数据数组和一个包含 2 个部分的 TableView :一个部分用于数组的总数,另一个用于数组中的每个数字。这个项目的代码太长太复杂,我无法提出简单的代码修复,所以这将是一个更笼统的问题。
当单元格输出到屏幕时,它们的格式正确(输出到 TableView
中的正确部分和单元格)。第二部分输出数组中的每个数字,每行一个数字,但每次输入新数字时,顶部部分都会在新行中重复“数字总数为:_”或写在它下面的第二部分。
例如:
表格应该是这样的-
数字总数为:3
1
2
6
但实际上最终发生的是:
数字总数为:3
数字总数为:3
数字总数为:3
1
2
6
如何控制输出第一部分的次数,同时仍保留由 numberOfRowsInSection:
方法中的 [array count]
确定的第二部分?
**如果您需要进一步说明,请随时询问,我知道这很含糊。
**更新**
现在,当用户输入的数组为 nil 时,我需要消除“index 0 beyond bounds of empty array”错误消息。以下是目前 numberOfRowsInSection:
方法的代码:
if (timeline != nil){
int a = 0;
switch (section) {
case 0:
a = 1;
break;
case 1:
a = timeline.count;
break;
}
return a;
}
else {
return 1;
}
我应该进一步改变什么来防止这两个问题的发生?
最佳答案
你已经定义了两个部分,所以在里面 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if (section == 0) {
return 1;
} else {
return [SomeArray count];
}
如果您想为每个表格部分添加单独的标题,模式类似: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section ==0 ) {
return @"Title For Section 1";
} else {
return @"Title For Section 2";
}
关于ios - 控制 TableViewCell 输出到 TableView 的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24375407/
相关文章:
javascript - UIWebView:我可以在任何网页中禁用 javascript alert() 吗?
ios - NSCalendar [_NSCopyOnWriteCalendarWrapper 组件 :fromDate:]: error
ios - 带自动布局的 tableHeaderView 无法正常工作
iphone - iOS TabBar 图标设计技巧和实现 : How To?
ios - Swift AVAudioPlayer 从头开始重启
ios - 如何抑制变暖 "Mehtod ' XXXX' 无限递归”
objective-c - 如何使用 Core Graphics 和 CALayer 绘制更精确的线条