2

我有UITableView大量数据。我UITableViewCell每次都像这样创建:

 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCellIdentifier]; 

之后,我将许多UIView作为子视图添加到UITableViewCell例如

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button addTarget:self action:@selector(onDocumentLogoTapped:) forControlEvents:UIControlEventTouchUpInside];
                button.frame = imageRect;
                button.tag = indexPath.row;
                [cell addSubview:button];

就像这样,我还添加了UIImageView. 问题是由于内存问题,我UITableView变得非常慢并且应用程序有时会崩溃。

任何人都可以建议我UITableViewCell在添加 subView 时使用正确的方法吗?

4

3 回答 3

4

cellForRowAtIndexPath方法中使用它:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

}
于 2013-02-22T06:25:27.720 回答
1

像下面这样:

在添加子视图之前,首先删除以前的子视图

for(UIView *sv in [cell.contentView subViews])
{
   [sv removefromsuperview];
}

[cell.contentView addSubview:button];

希望这个帮助!

于 2013-02-22T06:36:40.743 回答
0

我认为如果您使用自定义单元格会更好,那么您可以使用此方法作为

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

            NSArray *nib;
        static NSString *cellIdentifier= @"cell";

        UITableViewCell *theCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


        if(theCell== nil)
        {
            {
                nib = [[NSBundle mainBundle] loadNibNamed:@"<your custom cell xib name>" owner:self options:nil]; 
            }
            theCell = [nib objectAtIndex:0];

             theCell.selectionStyle = UITableViewCellSelectionStyleBlue;
        }

        UIImageView *imageView=(UIImageView*)[[theCell contentView] viewWithTag:101];
//assuming it has a image view with tag 101 you can use any thing

}
于 2013-02-22T06:21:13.793 回答