#iphone #uitableview #uilabel
#iPhone #uitableview #uilabel
Вопрос:
Я помещаю несколько UILabels внутри каждой ячейки в UITableView вместо одной ячейки.textLabel.text. Затем я использую reloaddata для размещения новых uilabels. Как мне избавиться от старых меток?
редактировать: Если я помещаю 5 меток в ячейку, а затем перезагружаю ячейку, используя только 2 метки, с момента последнего вызова cellForRowAtIndexPath остается еще 3 метки. Если я использую viewWithTag, как сказал Голдин, я могу повторно использовать старые метки, но могу ли я удалить ненужные метки из памяти?
редактировать: это мой метод
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(j*50.0, 0, 49.0,logicTable.rowHeight)] autorelease];
label.tag = 1;
label.text = [NSString stringWithFormat:@"ABC"];
label.textAlignment = UITextAlignmentCenter;
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
return cell;
}
Комментарии:
1. Ваш вопрос неясен. Требуется дополнительное объяснение.
Ответ №1:
Похоже, что вы делаете, это в вашем методе cellForRowAtIndexPath вы настраиваете свои UITableViewCells с некоторыми метками в них, и каждый раз вы создаете метки с нуля. Что вам следует сделать, так это настроить метки, если вы создаете новую ячейку, а затем установить значения в метках за пределами этой, чтобы в полной мере использовать возможность повторного использования ячеек табличного представления для повышения производительности прокрутки табличного представления.
Ключевым является метод, -viewWithTag:
который вместе со tag
свойством UIView вы можете использовать для поиска определенного вложенного представления.
Небольшой пример кода:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCellIdentifier";
UITableViewCell *cell = (WHArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *firstLabel = nil;
UILabel *secondLabel = nil;
UILabel *thirdLabel = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
firstLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0.0, 0.0, 20.0, 20.0)] autorelease];
firstLabel.tag = 1;
[cell addSubview:firstLabel];
secondLabel = [[[UILabel alloc] initWithFrame: CGRectMake(20.0, 0.0, 20.0, 20.0)] autorelease];
secondLabel.tag = 2;
[cell addSubview:secondLabel];
thirdLabel = [[[UILabel alloc] initWithFrame: CGRectMake(40.0, 0.0, 20.0, 20.0)] autorelease];
thirdLabel.tag = 3;
[cell addSubview:thirdLabel];
}
else
{
firstLabel = (UILabel *)[cell viewWithTag:1];
secondLabel = (UILabel *)[cell viewWithTag:2];
thirdLabel = (UILabel *)[cell viewWithTag:3];
}
firstLabel.text = @"First Label's Text Here";
secondLabel.text = @"Second Label's Text Here";
thirdLabel.text = @"Third Label's Text Here";
return cell;
}
Комментарии:
1. Спасибо. Я думал, что как только я выйду из cellForRowAtIndexPath, метка будет потеряна навсегда. Я пробую это