0

我需要在填充了自定义单元格的表格顶部加载更多单元格,第一次填充表格时一切正常,没有重叠也没有问题,如果我尝试加载更多单元格,则此单元格出现但高度错误如果我尝试随机滚动单元格更改位置!这是我的代码,你哪里出错了?

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

    return [self.arrayMessaggi count] + 1;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (indexPath.row == 0) {

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

            UILabel *labelLoad = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            [labelLoad setBackgroundColor:[UIColor clearColor]];
            [labelLoad setTextAlignment:NSTextAlignmentCenter];
            [labelLoad setText:[NSString stringWithFormat:@"%d - Load more...",indexPath.row]];
            [labelLoad setText:@"Load more..."];
            [labelLoad setTag:3];
            [cell.contentView addSubview:labelLoad];
        }

        UILabel *labelLoad = (UILabel *)[cell.contentView viewWithTag:3];
        [labelLoad setText:[NSString stringWithFormat:@"%d - Load more...",indexPath.row]];

    }
    else {

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

            PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
            PFUser *user = [obj objectForKey:@"daUtente"];

            float height = [self getStringHeight:[obj objectForKey:@"TestoMessaggio"] andFont:[UIFont systemFontOfSize:13]];

            UILabel *labelUser = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
            [labelUser setBackgroundColor:[UIColor clearColor]];
            [labelUser setText:[user objectForKey:@"username"]];
            [labelUser setFont:[UIFont systemFontOfSize:13]];
            [labelUser setTag:1];
            [cell.contentView addSubview:labelUser];

        }

        PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
        PFUser *user = [obj objectForKey:@"daUtente"];

        UILabel *labelUser = (UILabel *)[cell.contentView viewWithTag:1];

        [labelUser setText:[NSString stringWithFormat:@"%d - %@",indexPath.row,[user objectForKey:@"username"]]];


    }

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    float height = 44;

    if (indexPath.row > 0) {

        PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
        NSString *text = [obj objectForKey:@"TestoMessaggio"];
        height = 30 + [self getStringHeight:text andFont:[UIFont systemFontOfSize:13]] + 10;
    }

    return height;
}

- (void) setPullDownToRefresh {

    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(aggiornaTabella:) forControlEvents:UIControlEventValueChanged];
    [self.messagesTableView addSubview:refreshControl];

}
- (void) aggiornaTabella:(UIRefreshControl *)myRefreshControl {

    self.arrayMessaggi = [NSMutableArray arrayWithArray:objects];
    self.messagesTableView = [[UITableView alloc] init];
    [self.messagesTableView reload data];
}
4

0 回答 0