Search code examples
iphoneuitableviewxcode4uilabel

UILabel in Table View - the right way?


I was trying to implement a UILabel into a cell and what I get is overlapping of some values when I scroll the table up and down a couple of times. I work with ARC so there is no release when I want, so my question is : What's the right way of implementing a Label into a tableView cell?

Here is how it looks

- (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];


    }
     // Configure the cell...  

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
    cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1]; 


    UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, cell.frame.size.width, cell.frame.size.height)];
    cellLabelS1.backgroundColor = [UIColor clearColor];
    cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
    [cellLabelS1 setTextColor:[UIColor whiteColor]];


    [cellLabelS1 setText:temperatureString];
    temperatureString = nil;
    [cell addSubview:cellLabelS1];
    [[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
    [[cell textLabel]setText:cityString];

    return cell;

}


Solution

  • - (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];
    
            UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0,cell.frame.size.width, cell.frame.size.height)];
    cellLabelS1.backgroundColor = [UIColor clearColor];
    cellLabelS1.tag = 200; 
    cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
    [cellLabelS1 setTextColor:[UIColor whiteColor]];
    [cell addSubview:cellLabelS1];
    }
     // Configure the cell...  
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
    cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1]; 
    
    UILabel *cellLabelS1 = (UILabel*)[cell viewWithTag:200];
    [cellLabelS1 setText:temperatureString];
    temperatureString = nil;
    
    [[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
    [[cell textLabel]setText:cityString];
    
    return cell;
    }
    

    may this will help you....