ios - numberOfRowsInSection 计数将所有部分行设置为第一个部分计数

标签 ios json uitableview

我正在尝试设置此分段表以在不同部分中显示不同的 JSON 数组,但出于某种原因,如果我在第一部分之后的任何部分中有更大的行数,我会得到 [__NSArrayM objectAtIndex:]: index 3 Beyond边界 [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;

}

最佳答案

那是因为您从切换部分检索了 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 个团队怎么办?想想看!希望有所帮助:)

关于ios - numberOfRowsInSection 计数将所有部分行设置为第一个部分计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23771567/

相关文章:

json - 如何使用 libcurl 发布 JSON 缓冲区?

json - 在go中解析动态json

swift - 如何在 Controller 中使用两个按钮?每个按钮都有一个桌面 View ?

ios - UITableView Cell 不应水平快速滚动 iOS

ios - XCUITest如何访问tableView表头

ios - 检索用户喜欢的媒体不会返回任何内容

ios - 单击后退按钮时,导航堆栈中 VC 的 UINavigationBar 标题颜色不会更改

javascript - JSON Javascript 到 Flash 通信问题(使用外部接口(interface)): Hardcoded string works, 否则不会

iphone - MFMailComposeViewController 无法将 URL 识别为链接

Android Xamarin 使推送通知不创建新 Activity 而是使用当前 Activity