我正在尝试将此分区表设置为在不同的部分显示不同的JSON数组但由于某种原因,如果我在第一部分后的任何部分中有更大的行数,我得到[__NSArrayM objectAtIndex:]:索引3超出界限[0 .. 2]。 这就是我如何设置每个部分的行数,在viewdidload上设置数组,并且在我填充所有数组后调用重新加载
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch (section) {
case 0:
return [_team1 count];
break;
case 1:
return [_team2 count];
break;
case 2:
return [_team3 count];
break;
case 3:
return [_team4 count];
break;
case 4:
return [_team5 count];
break;
default:
return section;
break;
}
}
对于cellForRowAtIndex,我有这个,Team1 - Team5是我用来从JSON填充数组的字典类:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
Team1 *team1= [_team1 objectAtIndex:indexPath.row];
Team2 *team2= [_team2 objectAtIndex:indexPath.row];
Team3 *team3= [_team3 objectAtIndex:indexPath.row];
Team4 *team4= [_team4 objectAtIndex:indexPath.row];
Team5 *team5= [_team5 objectAtIndex:indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
[tableView registerNib:[UINib nibWithNibName:@"TeamCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
}
isShowingDetails = NO;
for(UILabel *v in cell.details)
{
v.alpha = 0;
}
cell.clipsToBounds = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
if(indexPath.section == 0){
[cell.name setText:team1.name];
[cell.institute setText:team1.institute];
[cell.line setText:team1.line];
[cell.email setText:team1.email];
[cell.mobile setText:team1.mobile];
[cell.postal setText:team1.postal];
[cell.instituteAdd setText:team1.office];
[cell.instituteWeb setText:team1.web];
[cell.linkedWeb setText:team1.link];
}
if(indexPath.section == 1)
{
[cell.institute setText:team2.institute];
[cell.line setText:team2.line];
[cell.email setText:team2.email];
[cell.mobile setText:team2.mobile];
[cell.postal setText:team2.postal];
[cell.instituteAdd setText:team2.office];
[cell.instituteWeb setText:team2.web];
[cell.linkedWeb setText:team2.link];
}
if(indexPath.section == 2)
{
[cell.institute setText:team3.institute];
[cell.line setText:team3.line];
[cell.email setText:team3.email];
[cell.mobile setText:team3.mobile];
[cell.postal setText:team3.postal];
[cell.instituteAdd setText:team3.office];
[cell.instituteWeb setText:team3.web];
[cell.linkedWeb setText:team3.link];
}
if(indexPath.section == 3)
{
[cell.institute setText:team4.institute];
[cell.line setText:team4.line];
[cell.email setText:team4.email];
[cell.mobile setText:team4.mobile];
[cell.postal setText:team4.postal];
[cell.instituteAdd setText:team4.office];
[cell.instituteWeb setText:team4.web];
[cell.linkedWeb setText:team4.link];
}
if(indexPath.section == 4)
{
[cell.institute setText:team5.institute];
[cell.line setText:team5.line];
[cell.email setText:team5.email];
[cell.mobile setText:team5.mobile];
[cell.postal setText:team5.postal];
[cell.instituteAdd setText:team5.office];
[cell.instituteWeb setText:team5.web];
[cell.linkedWeb setText:team5.link];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
答案 0 :(得分:2)
这是因为您从切换部分中检索了Team
的详细信息。把它移到里面。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
[tableView registerNib:[UINib nibWithNibName:@"TeamCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
}
isShowingDetails = NO;
for(UILabel *v in cell.details)
{
v.alpha = 0;
}
cell.clipsToBounds = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
if(indexPath.section == 0){
Team1 *team1= [_team1 objectAtIndex:indexPath.row];
[cell.name setText:team1.name];
[cell.institute setText:team1.institute];
[cell.line setText:team1.line];
[cell.email setText:team1.email];
[cell.mobile setText:team1.mobile];
[cell.postal setText:team1.postal];
[cell.instituteAdd setText:team1.office];
[cell.instituteWeb setText:team1.web];
[cell.linkedWeb setText:team1.link];
}
if(indexPath.section == 1)
{
Team2 *team2= [_team2 objectAtIndex:indexPath.row];
[cell.institute setText:team2.institute];
[cell.line setText:team2.line];
[cell.email setText:team2.email];
[cell.mobile setText:team2.mobile];
[cell.postal setText:team2.postal];
[cell.instituteAdd setText:team2.office];
[cell.instituteWeb setText:team2.web];
[cell.linkedWeb setText:team2.link];
}
if(indexPath.section == 2)
{
Team3 *team3= [_team3 objectAtIndex:indexPath.row];
[cell.institute setText:team3.institute];
[cell.line setText:team3.line];
[cell.email setText:team3.email];
[cell.mobile setText:team3.mobile];
[cell.postal setText:team3.postal];
[cell.instituteAdd setText:team3.office];
[cell.instituteWeb setText:team3.web];
[cell.linkedWeb setText:team3.link];
}
if(indexPath.section == 3)
{
Team4 *team4= [_team4 objectAtIndex:indexPath.row];
[cell.institute setText:team4.institute];
[cell.line setText:team4.line];
[cell.email setText:team4.email];
[cell.mobile setText:team4.mobile];
[cell.postal setText:team4.postal];
[cell.instituteAdd setText:team4.office];
[cell.instituteWeb setText:team4.web];
[cell.linkedWeb setText:team4.link];
}
if(indexPath.section == 4)
{
Team5 *team5= [_team5 objectAtIndex:indexPath.row];
[cell.institute setText:team5.institute];
[cell.line setText:team5.line];
[cell.email setText:team5.email];
[cell.mobile setText:team5.mobile];
[cell.postal setText:team5.postal];
[cell.instituteAdd setText:team5.office];
[cell.instituteWeb setText:team5.web];
[cell.linkedWeb setText:team5.link];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
顺便说一下,当你为5个团队创建5个类而它们具有相同的属性并且代表相同的实体时,这不是优化的。如果你有超过5支队伍怎么办?想一想!希望有所帮助:)
答案 1 :(得分:-1)
这个错误非常明显:
Team1 *team1= [_team1 objectAtIndex:indexPath.row];
Team2 *team2= [_team2 objectAtIndex:indexPath.row];
Team3 *team3= [_team3 objectAtIndex:indexPath.row];
Team4 *team4= [_team4 objectAtIndex:indexPath.row];
Team5 *team5= [_team5 objectAtIndex:indexPath.row];
让我们说第4节(_team5)有3行0到2,而其他只有2行。最终,您的方法将被调用以获取第4节的第2行._team5在索引2处有一个对象,但其他团队不会,所以您崩溃了。
NSArray* teams = nil;
switch (indexPath.section) {
case 0: teams = _team1; break;
case 1: teams = _team2; break;
case 2: teams = _team3; break;
case 3: teams = _team4; break;
case 4: teams = _team5; break;
}
等。
对于Team1,Team2,Team3,Team4,Team5,你真的没有不同的课程,对吗?我的意思是肯定不是吗?那太荒谬了。