首页 > 解决方案 > cellForRowAtIndexPath 忽略部分

问题描述

在我的 UITableViewController 中,我定义了 3 个部分。但是,如果我想访问 cellForRowAtIndexPath 方法中比第一个方法更多的部分,这将被忽略。例如,如果我想在第二部分添加一行。有没有人有解决这个问题的办法?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.section == 0) {
    if (indexPath.row == 0) {
        ..
        return cell;
    }
    if (indexPath.row == 1) {
        ..
        return cell;
    }
    if (indexPath.row == 2) {
        ..
        return cell;
    }
}
if (indexPath.section == 1) {
            UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:aufgabe];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aufgabe];
    }
    [cell.textLabel setText:@"Test"];
    }
}
else if (indexPath.section == 2) {
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:geraet];
    return cell;
}
   return nil;
}

标签: objective-cuitableview

解决方案


有趣的!我为自己修好了。我必须填写 numberOfRowsInSection 方法。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   if (section == 0) {
       return 3;
   }
   if (section == 1) {
       return 5;
   }
   else {
       return 0;
   }
}

推荐阅读