在我的viewDidLoad
中,我设置了一系列议程项目,如下所示:
self.agendaTableArray = @[@"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"Dinner with Rebekah", @"Soccer game", @"Denist appt.", @"Celebrate job offer, drinks with Pierre!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!", @"No events today!"];
我想要的是这个数组中的每个字符串都在一个单独的单元格中,并且每个单元格都在它自己的部分中。这是我cellForRowAtIndexPath
到目前为止的样子:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
}
cell.textLabel.text = self.agendaTableArray[indexPath.section];
cell.textLabel.font = [UIFont systemFontOfSize:14];
[self whatSectionsAreVisible];
return cell;
}
问题是这段代码会抛出SAME部分下的不同单元格中的所有字符串,而不是它们自己的部分。如果我尝试使用错误*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
我设置此表导致这两个问题的方式有何不正确之处?
答案 0 :(得分:3)
确保您返回1
表示每个部分中的行数以及代表中部分数量的数组大小:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.agendaTableArray count];
}