user3140296
user3140296

Reputation: 169

Custom labels in tableviewcell

Im trying to make a custom label instead of the standard textabel and detailtextlabel.

i've tried doing this by creating a label called "labelNumb", but it wont print it in the cells how come and how can i position this label in the cell?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    cell.userInteractionEnabled = NO;



    UILabel *labelNumb = [[UILabel alloc] init];
    [cell.contentView addSubview:labelNumb];
    labelNumb.text = [NSString stringWithFormat:@"%@",[scoreArray2 objectAtIndex:indexPath.row]];
    return cell;
}

Upvotes: 0

Views: 55

Answers (2)

Sam Fischer
Sam Fischer

Reputation: 1472

  1. Check if cell is nil, and if it is create a new UITableViewCell with the same identifier.
  2. Make a new frame, add the UILabel to it, and pass it in the addSubview: method.

Upvotes: 0

Aaron Brager
Aaron Brager

Reputation: 66302

You haven't set the frame for labelNumb.

Also, if (cell == nil) will never happen since dequeueReusableCellWithIdentifier:forIndexPath: is guaranteed to return a cell.

Also, this will mess up as you start scrolling, since you're adding a new UILabel every time, instead of reusing the existing one.

Upvotes: 1

Related Questions