我使用dequeueReusableCellWithIdentifier:
的{{1}}方法遇到了一个奇怪的问题。不确定我是否能够很好地理解这种方法,或者只是很奇怪。这是:
我正在使用UITableView
向用户提供一些数据,并在我的内部
UITableView
我像这样使用dequeue方法:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
之后,我将一些子视图添加到单元格的UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
属性中。当我在桌面上向下滚动一点时,我会看到之前添加的子视图,即单元格不是空的,而是填充了#34; old"数据。如果我没有出列,并且每次只是contentView
一个新的,那么单元格是空的,但我确实看到了更多的内存消耗,这正是我试图降低一点。如果这意味着什么,我会使用ARC。
我应该怎样或如何解决这个问题?我已经尝试在内容视图和alloc-init
的子视图中运行for
循环,它会删除以前的视图并降低内存消耗。但这真的有必要吗?或者有更好的方法吗?
编辑以下是我添加子视图的更多代码
[view removeFromSuperview]
在你开始批评我为什么不使用单cell.backgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.backgroundColor = kClearColor; //defined to [UIColor clearColor]
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0)
{
UIImageView *shine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
shine.image = [UIImage imageNamed:@"top_shine_1"];
[cell.backgroundView addSubview:shine]; //its a gradient thats why its added to background
UILabel *appLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, winSize.height * 0.027, 250, 33)];
appLabel.backgroundColor = kClearColor; //defined to clear color
appLabel.textColor = kWhiteColor; //defined to white color
appLabel.text = [viewOrder objectAtIndex:tableView.tag]; //just an array from where I get the required text
appLabel.font = kStandardFontOfSize(30); //defined to a specific font
[cell.contentView addSubview:appLabel];
UIButton *settingsButton = [UIButton buttonWithType:UIButtonTypeCustom];
settingsButton.frame = CGRectMake(10, winSize.height * 0.0377, 31, 21);
[settingsButton setImage:[UIImage imageNamed:@"settings_button"] forState:UIControlStateNormal];
[settingsButton addTarget:self action:@selector(settings:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:settingsButton];
return cell; //here I just return it since this is all the config the first cell needs
}
NSString *app = [viewOrder objectAtIndex:tableView.tag];
NSArray *boxes = [[plist secondObjectForKey:@"order" parent:app] componentsSeparatedByString:@";"];
//Add necessary shines or create the last logotype cell - just some details and stuff, all are just images
if (indexPath.row == 1)
{
UIImageView *shine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 282.5)];
shine.image = [UIImage imageNamed:@"top_shine_2"];
[cell.backgroundView addSubview:shine];
}
else if (indexPath.row == 2)
{
UIImageView *shine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winSize.width, 150)];
shine.image = [UIImage imageNamed:@"main_shine"];
[cell.backgroundView addSubview:shine];
}
else if (indexPath.row == boxes.count + 1)
{
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(111.5, 25, 97, 20)];
logo.image = [UIImage imageNamed:@"cell_logo"];
[cell.backgroundView addSubview:logo];
return cell;
}
NSString *databox = [boxes objectAtIndex:indexPath.row - 1];
UIView *view; //Main subview to be added to the cell
/*
here I have a class that creates a view with a bunch of subviews added to that view, the view is then assigned to 'view'; kinda like
view = [someAssembler assembleViewWith:options.....]. all are basically UILabels or ImageViews added to the main view
*/
[cell.contentView addSubview:view]; //and here this 'main view' is added as a subview, this view is still visible after the cell has been dequeued and the shines are as well
return cell;
作为背景和文字颜色之前,让我提醒你,这仍处于测试阶段,稍后会处理。
答案 0 :(得分:2)
[cell.backgroundView addSubview:shine];
这些代码行是你的问题。
您应该在if(!cell)块中创建一个完整的可重用单元格,并在每次调用cellForRow
时重新填充它们。对于每个唯一的小区,应使用唯一的重用标识符。例如,如果您有多个具有不同布局子视图的单元格,则应为它们使用不同的标识符。
在您的具体示例中,必须在if (indexPath.row == 1)
块中创建单元格。
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = nil;
if (indexPath.row == 0) {
cellIdentifier = @"topCell";
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if (!cell) {
// create the cell and add the necessary subviews for indexPath row 0
}
return cell;
}
else if (indexPath.row == 1) {
}
//etc.
}
你必须使用这种方法为!cell块中的每个单元格创建“主子视图”,所以你应该考虑对一个单元格进行子类化。