Why we are checking if (cell == nil) in UITableViewController?

前端 未结 2 1976
梦毁少年i
梦毁少年i 2021-01-18 04:52

I am trying to implement UITableView based Application.For that I select UITableViewStyle is Group.In my TableView their is 15 section each section having 1

相关标签:
2条回答
  • 2021-01-18 05:16

    The test if (cell == nil) handles the case where there are no reusable cells to dequeue, in which case you must create a new cell. When you create the new cell, you are responsible for constructing its view hierarchy.

    0 讨论(0)
  • 2021-01-18 05:19
    NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row];
    
    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    This can be used instead of

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

    this is the simple example to write inside the cell ==nill

    - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    cell= nil;
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview]; 
    
        UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)];
        [selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"strip_s12A-1_h.png"]]];
        cell.selectedBackgroundView = selectview;
        [selectview release];
    
        UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)];
        //cellTitle.adjustsFontSizeToFitWidth=YES;
        [cellTitle setBackgroundColor:[UIColor clearColor]];
        [cellTitle setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:17]];
        [cellTitle setTextColor:[UIColor blackColor]];
        cellTitle.tag = 100+indexPath.row;
    
            cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:@"Des2"];
    
        [cell.contentView addSubview:cellTitle];
    
        [cellTitle release];
    }
    return cell;
         }
    

    i think will be enough right

    0 讨论(0)
提交回复
热议问题