uitableview cell creation on scrolling

i am using uitableview and i am facing a problem in cellforrow delegate i use this code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"mycell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell =  (CustomCell *)[topLevelObjects objectAtIndex:2];
    cell.btn_Selected.hidden=YES;
    cell.btn_Selected.tag=indexPath.row+1;

    [array_btnContainer addObject:cell.btn_Selected];

}

my problem is that when i run app it load my table view and create cells but when i scroll my table view it creates one more cell why ???? it must reuse the already created cell and not to enter (cell==nil) block but when ever i scroll it creates one cell and reuse other cell why???? i am stuck

Answers


This can be achieved via following these steps

  1. Create a reference of your CustomCell in .h file which you are using to show the tableview whatever it is lets called it ShowTableView.h. IBOutlet CustomCell *cell;

  2. Go to CustomCell.xib and select the fileowner then set the class property as ShowTableView.

  3. Attached the cell reference with the CustomCell

  4. Select the CustomCell and then set property Identifier value with mycell

  5. Now go to your ShowTableView.m file and cellForRowAtIndexPath method place this code:

     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:      (NSIndexPath *)indexPath
    {
    
    static NSString *kCellIdentifier = @"mycell";
    cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (!cell)
    {
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];      
    }  
    }
    

Now it will reuse your previous cells


In CustomCell.m you must overriding the following method as follows.

-(NSString*) reuseIdentifier{
       return @"mycell";
 }

Need Your Help

WooCommerce - how do I disable attribute option menus until the one above is selected?

javascript php jquery wordpress woocommerce

On the Product page of a Variable Product, I'd like to use javascript/jQuery to control the attribute drop down menus.

iOS - new line character in UITextView

ios uitextview newline

Reading in text from a data file. How can I get line breaks to work? I've tried \n, but it just puts "\n" into the string instead of creating a new line.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.