只有2个单元格,UITableView非常糟糕

时间:2011-10-19 21:09:14

标签: objective-c ios cocoa-touch uitableview

我有一个UITableView,我正在向单元格添加自定义视图。它真的落后了,只有2个细胞。这就是它的样子,当它向上和向下滚动时,我可以看到它断断续续/滞后(这是在iPhone 4s上)。我查看了App Store应用程序,并且非常复杂的tableviewcell的滚动非常流畅。我做错了吗?

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    int row = [indexPath row];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];


        UILabel *labelBarcode = [[UILabel alloc] initWithFrame:CGRectMake(80, 7, 150, 15)];
        [labelBarcode setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
        [labelBarcode setTag:10];

        UILabel *labelLength = [[UILabel alloc] initWithFrame:CGRectMake(80, 22, 150, 15)];
        [labelLength setFont:[UIFont fontWithName:@"Helvetica" size:14]];
        [labelLength setTag:11];

        UILabel *labelThickness = [[UILabel alloc] initWithFrame:CGRectMake(80, 37, 150, 15)];
        [labelThickness setFont:[UIFont fontWithName:@"Helvetica" size:14]];
        [labelThickness setTag:12];

        UILabel *labelDiameter = [[UILabel alloc] initWithFrame:CGRectMake(190, 22, 100, 15)];
        [labelDiameter setFont:[UIFont fontWithName:@"Helvetica" size:14]];
        [labelDiameter setTag:13];

        UILabel *labelCoating = [[UILabel alloc] initWithFrame:CGRectMake(190, 37, 100, 15)];
        [labelCoating setFont:[UIFont fontWithName:@"Helvetica" size:14]];
        [labelCoating setTag:14];

        UIImageView *thumbView = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 65, 50)];
        [thumbView setContentMode:UIViewContentModeScaleToFill];
        [thumbView setTag:15];


        [[cell contentView] addSubview:thumbView];
        [[cell contentView] addSubview:labelBarcode];
        [[cell contentView] addSubview:labelLength];
        [[cell contentView] addSubview:labelThickness];
        [[cell contentView] addSubview:labelDiameter];
        [[cell contentView] addSubview:labelCoating];

        [thumbView release];
        [labelBarcode release];
        [labelLength release];
        [labelThickness release];
        [labelDiameter release];
        [labelCoating release];

    }
    UILabel *labelBarcode = (UILabel *)[[cell contentView] viewWithTag:10];
    UILabel *labelLength = (UILabel *)[[cell contentView] viewWithTag:11];
    UILabel *labelThickness = (UILabel *)[[cell contentView] viewWithTag:12];
    UILabel *labelDiameter = (UILabel *)[[cell contentView] viewWithTag:13];
    UILabel *labelCoating = (UILabel *)[[cell contentView] viewWithTag:14];
    UIImageView *thumbView = (UIImageView *)[[cell contentView] viewWithTag:15];

    NSDictionary *pipe = [pipes objectAtIndex:row];


    NSString *stringBarcode = [pipe objectForKey:@"Barcode"];
    NSString *stringLength = [NSString stringWithFormat:@"Length: %@", [pipe objectForKey:@"Length"]];
    NSString *stringThickness = [NSString stringWithFormat:@"Wall: %@",[pipe objectForKey:@"Thickness"]];
    NSString *stringCoating = [NSString stringWithFormat:@"Coating: %@", [pipe objectForKey:@"Coating"]];
    NSString *stringDiameter = [NSString stringWithFormat:@"Diameter: %@",[pipe objectForKey:@"Diameter"]];


    [labelBarcode setText:stringBarcode];
    [labelLength setText:stringLength];
    [labelThickness setText:stringThickness];
    [labelDiameter setText:stringDiameter];
    [labelCoating setText:stringCoating];
    [thumbView setImage:[UIImage imageWithData:[pipe objectForKey:@"Thumbnail"]]];   

    return cell;
}

特别是当我加载UITableViewController时会出现延迟。请注意,我没有分配任何内容,我只在应用程序加载时分配数据(来自plist),整个应用程序内存消耗为1.56 MB。

3 个答案:

答案 0 :(得分:0)

这行代码:

[thumbView setImage:[UIImage imageWithData:[pipe objectForKey:@"Thumbnail"]]];   

阻塞并且可能需要花费大量时间,具体取决于数据量。在后台线程上执行此操作(可能通过NSOperation),然后在准备好映像时重新加载行。

答案 1 :(得分:0)

尝试放

[thumbView setImage:[UIImage imageWithData:[pipe objectForKey:@"Thumbnail"]]];

在重用条件内。

答案 2 :(得分:0)

我遇到了类似的问题,因为图像视图正在进行某种类型的调整。我先检查一下。如果你的thumbView正在调整大小,那么它本身会导致表格在滚动时滞后。如果是这种情况,您可以尝试将调整大小的图像预渲染到正确的大小。我有点假设只有两个单元格你没有让你的任何单元格实际上从表中拉出来。关于创建图像的时间的其他答案仅在您多次执行时才真正适用,这仅在重复使用单元格时才会发生。