Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I addSubview to cell.contentView?
    primarykey
    data
    text
    <p>A (when the cell is newly created):</p> <pre><code>- (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; } </code></pre> <p>or B (every time when the cell is found):</p> <pre><code>- (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; } </code></pre> <p>A or B? Thanks!</p> <p><strong>UPDATE</strong> Solution (thanks for the answers):</p> <pre><code>- (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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload