-3

滚动不堆积时如何在tableViewCell中创建数据?

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

    static NSString *theCell = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:theCell];

    if (! cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:theCell];
    }

    UILabel *data1 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]WithFrame:CGRectMake(10, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
    [cell insertSubview:data1 atIndex:0];

    UILabel *data2 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"subjek"]]WithFrame:CGRectMake(50, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
    [cell insertSubview:data2 atIndex:1];

    UILabel *data3 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"shadowScore"]]WithFrame:CGRectMake(200, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
    [cell insertSubview:data3 atIndex:2];

    UILabel *data4 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"finalScore"]]WithFrame:CGRectMake(250, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
    [cell insertSubview:data4 atIndex:3];

    UILabel *data5 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"kkm"]]WithFrame:CGRectMake(300, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
    [cell insertSubview:data5 atIndex:4];

    UILabel *data6 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"Status"]]WithFrame:CGRectMake(350, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
    [cell insertSubview:data6 atIndex:5];

    UILabel *data7 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"teachernote"]]WithFrame:CGRectMake(400, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
    [cell insertSubview:data7 atIndex:6];

    return cell;
}
4

1 回答 1

6

问题是每次加载单元格时,您都会添加一个新的UILabel. 当细胞被回收时,这会成为一个问题,因为它们已经创建了标签并且您正在创建更多标签。

您应该进行子类化,在加载UITableViewCell时创建所需的UILabels 和布局,然后只需在cellForRowAtIndexPath:方法中设置信息。这使您可以回收单元并提高性能,同时保持布局并避免您发现的这种“堆叠问题”。

第二种选择

第二个不太理想的选项是将您的UILabel创建方法移动到if (!cell)块中并使用标签检索它们并将它们设置在该块之外。这不太便携且更脆弱,但是重复使用标签的相同有效效果将到位。它看起来像这样:

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

    static NSString *theCell = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:theCell];

    if (! cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:theCell];

        UILabel *data1 = [self createLabelText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]WithFrame:CGRectMake(10, 10, 150, 50) WithFont:[UIFont fontWithName:@"Arial" size:16] WithColor:[UIColor clearColor]];
        data1.tag = 1;
        [cell insertSubview:data1 atIndex:0];

        // ... Load Other labels and give unique tags

    }

    UILabel *data1 = [cell viewWithTag:1];
    data1 setText:[NSString stringWithFormat:@"%@",[[arrayUtama objectAtIndex:indexPath.row]objectForKey:@"nomer"]]];

    // ... Load other labels by tag and set text.

    return cell;
}
于 2013-08-26T15:00:22.503 回答