0

我正在尝试对我的最后一个单元格实施加载更多方法。经过试验,我终于可以对我的单元实现更多的负载(还没有功能)。但这似乎有点困难,因为我正在从 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;

}

加载更多单元格(读取给我更多!)在最后一个单元格上弹出(但不是创建一个新的最后一个单元格,只是在正常的最后一个单元格上,因此会干扰其内容),并且还会在 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]'
4

1 回答 1

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
    }
}  
于 2012-05-11T18:19:24.620 回答