iOS表格视图滚动时出现故障

时间:2012-03-16 17:01:23

标签: iphone ios uitableview scroll

Hy先生,我有一个问题,每个单元格上都有4个图像的桌面视图,我需要知道点击了哪个图像,所以我把图像放在按钮上

一切都很好,但是在滚动时它会粘住,而不会崩溃,只是坚持一秒钟,然后再滚动。有人可以帮忙吗? 这是代码:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

UIButton *b1, *b2, *b3, *b4;
UIImageView *lBut1,*lBut2,*lBut3,*lBut4;
if (cell == nil)        
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

b1 = [UIButton buttonWithType:UIButtonTypeCustom];
b1.frame = CGRectMake(y, y, x, x);
UIImage* img = [UIImage imageWithContentsOfFile:[dataArray objectAtIndex:indexPath.row*4]];
[b1 setBackgroundImage:img forState:UIControlStateNormal];
b1.tag = indexPath.row*4+1;
[b1 addTarget:self action:@selector(reportChoose:) forControlEvents:UIControlEventTouchUpInside];
lBut1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
lBut1.frame = CGRectMake(0, 0, x, x);
[lBut1 setAlpha:0.5];
[b1 addSubview:lBut1];
[lBut1 release];
[cell addSubview:b1];

if (indexPath.row * 4 + 1 < [dataArray count]) {
    b2 = [UIButton buttonWithType:UIButtonTypeCustom];
    b2.frame = CGRectMake(2*y+x, y, x, x);
    UIImage* img = [UIImage imageWithContentsOfFile:[dataArray objectAtIndex:indexPath.row*4 + 1]];
    [b2 setBackgroundImage:img forState:UIControlStateNormal];
    b2.tag = indexPath.row*4+2;
    [b2 addTarget:self action:@selector(reportChoose:) forControlEvents:UIControlEventTouchUpInside];
    lBut2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
    lBut2.frame = CGRectMake(0, 0, x, x);
    [lBut2 setAlpha:0.5];
    [b2 addSubview:lBut2];
    [lBut2 release];
    [cell addSubview:b2];
}

if (indexPath.row * 4 + 2 < [dataArray count]) {
    b3 = [UIButton buttonWithType:UIButtonTypeCustom];
    b3.frame = CGRectMake(3*y+2*x, y, x, x);
    UIImage* img = [UIImage imageWithContentsOfFile:[dataArray objectAtIndex:indexPath.row*4 + 2]];
    [b3 setBackgroundImage:img forState:UIControlStateNormal];
    b3.tag = indexPath.row*4+3;
    [b3 addTarget:self action:@selector(reportChoose:) forControlEvents:UIControlEventTouchUpInside];
    lBut3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Defaulte.png"]];
    lBut3.frame = CGRectMake(0, 0, x, x);
    [lBut3 setAlpha:0.5];
    [b3 addSubview:lBut3];
    [lBut3 release];
    [cell addSubview:b3];
}
if (indexPath.row * 4 + 3 < [dataArray count]) {
    b4 = [UIButton buttonWithType:UIButtonTypeCustom];
    b4.frame = CGRectMake(4*y+3*x, y, x, x);
    UIImage* img =[UIImage imageWithContentsOfFile:[dataArray objectAtIndex:indexPath.row*4 + 3]];
    [b4 setBackgroundImage:img forState:UIControlStateNormal];
    b4.tag = indexPath.row*4+4;
    [b4 addTarget:self action:@selector(reportChoose:) forControlEvents:UIControlEventTouchUpInside];
    lBut4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Defaulte.png"]];
    lBut4.frame = CGRectMake(0, 0, x, x);
    [lBut4 setAlpha:0.5];
    [b4 addSubview:lBut4];
    [lBut4 release];
    [cell addSubview:b4];
}

return cell;

}

3 个答案:

答案 0 :(得分:2)

细胞应该重复使用 滚动单元格时添加越来越多的按钮。分配和添加视图作为子视图需要时间 滚动时间足够长时,内存不足。

使用这样的模式:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UIButton *button;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 1001;
        // configure button properties that never change between rows
        button.frame = ...

        [cell.contentView addSubview:button];
    }
    UIImage *myImage = ...

    button = (UIButton *)[cell.contentView viewWithTag:1001];
    // configure properties that are different for each row
    [button setBackgroundImage:myImage forState:UIControlStateNormal];
    return cell;
}

仅在if (cell == nil)中添加子视图。在那种情况之外配置它们。

或者创建一个包含按钮的UITableViewCell子类。

答案 1 :(得分:0)

最好的方法是创建一个CUstom UITableViewCell,使您的工作更轻松。

您可以在自定义uitableviewcell类中编写选择器。

http://www.galloway.me.uk/tutorials/custom-uitableviewcell/

答案 2 :(得分:0)

这是一个非常常见的问题。我应该说两点:

1)重用tableView单元格。 (UITableView的常见方法) 2)尽可能多地缓存数据。缓存虽然增加了内存占用,但它也将改善滚动。虽然缓存水平必须通过适当的测试来确定(使用仪器)。