iphone - 滚动时 iOS TableView 故障

标签 iphone ios uitableview scroll

伙计们,我有一个问题,有一个每个单元格上有 4 个图像的 tableview,我需要知道点击了哪个图像,所以我将图像放在按钮上

一切正常,但它在滚动时卡住了,没有崩溃,只是卡住了想了一秒钟,然后再次滚动。有人可以帮忙吗? 这是代码:

-(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;

最佳答案

细胞应该被重复使用。
当单元格滚动时,您会添加越来越多的按钮。分配和添加 View 作为 subview 需要时间。
当您滚动足够长的时间时,您将耗尽内存。

使用这样的模式:

- (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) 中添加 subview 。在该条件之外配置它们。

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

关于iphone - 滚动时 iOS TableView 故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9741452/

相关文章:

ios - 通过单击应用程序图标直接启动应用程序时未获取推送通知数据

ios - 更改 Google map 大小 iOS Swift

ios - 对于特定对象,使一个数组等于另一个数组

ios - Swift 是否可以为 MKLocalSearchRequest 和 MKLocalSearchResponse 设置一个特定的国家

iphone - 在加载特定 View Controller 时禁用可达性 block 运行?

ios - 如何构建支持i386和armv7的ios框架

ios - 在后台执行游戏中的评分任务 Objective-c

iphone - 在 Interface Builder 和 XCode 中连接 UILabel?

ios - 动态 UITableView 中的 UISlider

ios - 单击swift 4中的单选按钮时如何获取 TableView 索引?