Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 row.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 15;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==12) 
    {
        return 120;
    }
    else
    {
        return 60;
    }

}

I want add a UITextView on Section 12

For that i did following 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];



    }
    if ([indexPath section] == 12) 
    {
        if([indexPath row]==0)
        {
            descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)];
            descriptionTextField.font = [UIFont systemFontOfSize:15.0];   
            descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];

            [descriptionTextField setDelegate:self];
            [descriptionTextField setTag:2];
            [descriptionTextField setText:@"Enter Location Description."];

            descriptionTextField.keyboardType=UIKeyboardTypeDefault;
            descriptionTextField.returnKeyType=UIReturnKeyNext;


            descriptionTextField.textColor=[UIColor blackColor];
            descriptionTextField.editable=YES;
            descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords;
            descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault;
            descriptionTextField.textAlignment=UITextAlignmentLeft;

            UIToolbar*  keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];

            UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(keyboardDoneButtonActin:)];
            [Done setWidth:65.0f];
            [keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ];
            [extra release];
            [Done release];

            [keboardToolBar setTintColor:[UIColor blackColor]];
            [keboardToolBar setAlpha:.70];
            [descriptionTextField setInputAccessoryView:keboardToolBar];
            [descriptionTextField setTag:101];
            [cell.contentView addSubview:descriptionTextField];
            [descriptionTextField release];
        }
    }

    return cell;
}

In the initil stage the table view like this

enter image description here

if i am scrolling tableview up and down , then the uitextView section changed and it will display multiple location.

enter image description here

I can't understand my fault, why this happend?

if am implementing the above code in side the if(cell==nil)

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

    if ([indexPath section] == 12) 
    {
        if([indexPath row]==0)
        {
           **/* implemention  all code here*/**

             [cell.contentView addSubview:descriptionTextField];
            [descriptionTextField release];
        }
    }

    return cell;

}

UITextView not disply, i think it is not allocating.

so what is the difference between code implementing in if (cell == nil) { inside }

if (cell == nil) { } out side

like image 966
Musthafa P P Avatar asked Mar 14 '12 11:03

Musthafa P P


1 Answers

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.

like image 176
FluffulousChimp Avatar answered Nov 15 '22 07:11

FluffulousChimp