A:Subview'ı cell.contentView'a nasıl eklemeliyim? (Hücre yeni oluşturulur)
- (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;
}
veya B (hücre bulunan her zaman):
- (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 ya da B? Teşekkürler!
GÜNCELLEME Çözüm (cevaplar için teşekkürler): Eğer A hücreyi oluştururken
- (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;
}
yapar B nedeni bellek sızıntısı oluşturabilir ya demek? – ohho
@ohho, Düşünme. Her şey bana iyi görünüyor. –
B, hücreye etiket eklemeye devam etmiyor mu? Biraz kaydırma yaptıktan sonra, alt gösterimlerinde birden fazla etiket tutan birden fazla hücre ile sonuçlanacağını düşünüyorum. Yanılıyorsam lütfen beni düzeltin. – Mike