ए (जब सेल नव निर्मित है):मुझे सेल.contentView में सबव्यूव्यू कैसे जोड़ना चाहिए?
- (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;
}
या बी (हर बार जब सेल पाया जाता है):
- (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;
}
ए या बी? धन्यवाद!
अद्यतन समाधान (जवाब के लिए धन्यवाद):
- (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;
}
प्रदर्शन इसके अलावा, करता है बी कारण स्मृति रिसाव बनाने? – ohho
@ohho, ऐसा मत सोचो। सब कुछ ठीक दिखता है। –
क्या बी सेल में लेबल जोड़ना नहीं चाहेगा? मुझे लगता है कि कुछ स्क्रॉलिंग के बाद, आप अपने सबव्यूज़ में एकाधिक लेबल रखने वाले एकाधिक सेल्स के साथ समाप्त हो जाएंगे। कृपया मुझे सुधारें अगर मैं गलत हूं। – Mike