tableView - 有时会出错,有时没有 - 尝试在最后一个单元格中加载标签

时间:2012-05-11 16:28:47

标签: ios tableview cell nsrangeexception

我正在尝试为我的上一个单元格实现一个加载更多的方法。经过实验,我终于可以实现更多的负载(没有功能)到我的单元格。但它似乎有点困难,因为我正在从XML加载我的内容。

以下是我的工作:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   if([tabelle count]>= 10)
{
    return [tabelle count]+1;
}
else
{
    return [tabelle count];

}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; }

    if(indexPath.row != [tabelle count] )
{

    cell.textLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
    cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
    self.tableView.separatorColor = [UIColor clearColor];

    NSString *TableText = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number1]]; 
    NSString *TableText2 = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number2]];  

    NSString *cellValue = [NSString stringWithFormat:@"%@", TableText2];
    NSString *cellValue2 = [NSString stringWithFormat:@"%@", TableText];
    cell.textLabel.text = cellValue;
    cell.detailTextLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
    cell.detailTextLabel.text = cellValue2;

}
else if(indexPath.row==[tabelle count])

{
    UILabel *loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,30)];
    loadMore.textColor = [UIColor blackColor];
    loadMore.backgroundColor = [UIColor clearColor];
    loadMore.font=[UIFont fontWithName:@"Verdana" size:20];
    loadMore.textAlignment=UITextAlignmentCenter;
    loadMore.font=[UIFont boldSystemFontOfSize:20];
    loadMore.text=@"Gimme more!";
    [cell addSubview:loadMore];
}
return cell;

}

加载更多单元格(更多地读取Gimme!)会弹出最后一个单元格(但不会创建新的最后一个单元格,只是在正常的最后一个单元格上,从而干扰其内容)并且还会在tableview上随机弹出。

第一个问题是,它使用这种方法,所以我不得不改变它,否则我得到以下错误:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [tabelle count]) {

} else {
    NSString *text = [[self.tabelle objectAtIndex:indexPath.row] number1];

    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 7, 1000.0f)];
    return textSize.height + PADDING * 7;
}
}

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
{
    if(indexPath.row==[tabelle count])
    {
        [self.tableView cellForRowAtIndexPath:indexPath];
    }
}      

} 这是我遇到的错误(有时滚动视图时仍然有):

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'

1 个答案:

答案 0 :(得分:1)

您发布的代码似乎有些奇怪。在第一个片段中,if语句似乎自己浮动(没有封装方法?)。在heightForRowAtIndexPath中,您没有从if语句的then部分返回值。

[编辑] 这是我写你到目前为止发布的内容的方式:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // I am not the below make sense but that depends on what you are intending
    // I do have a feeling there is a "off by one" issue here
    if ([tabelle count] >= 10) {
        return [tabelle count]+1;
    }else{
        return [tabelle count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (indexPath.row != [tabelle count] ) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1"];
        if (cell == nil) {
            [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell1"]; }

            cell.textLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
            cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
            cell.detailTextLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
            self.tableView.separatorColor = [UIColor clearColor];
        }
        NSString *TableText = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number1]]; 
        NSString *TableText2 = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number2]];  

        cell.textLabel.text = TableText;
        cell.detailTextLabel.text = TableText2;
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2"];
        if (cell == nil) {
            [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell2"]; }
            UILabel *loadMore = [[UILabel alloc] initWithFrame: CGRectMake(0,0,320,30)];
            loadMore.textColor = [UIColor blackColor];
            loadMore.backgroundColor = [UIColor clearColor];
            loadMore.textAlignment = UITextAlignmentCenter;
            loadMore.font = [UIFont boldSystemFontOfSize:20];
            loadMore.text = @"Gimme more!";
            [cell addSubview:loadMore];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == [tabelle count]) {
        return 44.0;
    }else{
        NSString *text = [[self.tabelle objectAtIndex:indexPath.row] number1];
        CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 7, 1000.0f)];
        return textSize.height + PADDING * 7;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == [tabelle count]) {
        // TO-DO: user tapped 'Giimie more'
    }else{
        // TO-DO: user tapped a regular cell
    }
}