Как мне добавить addSubview в cell.contentView?
A (когда ячейка вновь создана):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
label.text = @"9:00am";
[cell.contentView addSubview:label];
[label release];
}
return cell;
}
или B (каждый раз, когда ячейка найдена):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
label.text = @"9:00am";
[cell.contentView addSubview:label];
[label release];
return cell;
}
A или B? Спасибо!
ОБНОВЛЕНИЕ Решение (спасибо за ответы):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(0, 0, 160, 50);
label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
label.tag = 1;
[cell.contentView addSubview:label];
[label release];
} else {
label = (UILabel *) [cell viewWithTag:1];
}
label.text = [NSString stringWithFormat:@"%d", [indexPath row]];
return cell;
}
Ответы
Ответ 1
Это все о производительности. С помощью A вы повторно используете ячейку со всеми ее подзонами, с B, вы повторно используете только сырую ячейку и добавляете новое подчинение на каждую итерацию, что ИМХО не так хорошо, как A re: performance.
Я говорю либо создаю подкласс UITableView
, либо использую решение A.
Ответ 2
Вы должны добавлять только вспомогательные представления при создании ячейки, как в A, однако присваивайте значения меткам и т.д. каждый раз, как в B.
Это решение получилось бы естественным, если вы создадите свой собственный подкласс UITableViewCell, который добавит его собственные представления.
Что-то вроде этого.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(0, 0, 160, 50);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label];
[label release];
}
// Get a reference to the label here
label.text = @"9:00am";
return cell;
}
Таким образом вы получаете преимущества в производительности только для распределения подразделений один раз, и вы можете просто установить соответствующие свойства в подвью в случае необходимости.