Yrol
Yrol

Reputation: 169

ios - UITableView duplicate cells

I have the following code that creates a view in the app. However as I scroll, the bottom sections of the view gets replaced by the items of the very first section (one cell that contains an image), any suggestions? I have tried many things (specially with the dequeueReusableCellWithIdentifier) but none worked.

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0) {
            simpleCell *cell = (simpleCell *)[self.tableview dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
            cell.imageView.image = nil;
            cell.text.text = nil;
                //cell = [[simpleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"simple"];
                if(indexPath.row == 0){
                    cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];

                    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"isFacebookLoggegIn"]){
                        cell.imageView.image = [UIImage imageNamed:@"fb_tbl_connect.png"];
                    }else{
                        cell.imageView.image = [UIImage imageNamed:@"tbl_disconnect_fb.png"];
                    }
                }


                if(indexPath.row == 1){
                    cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
                    cell.text.text = @"Edit Your Profile";
                }
            return cell;
    }

        if (indexPath.section == 1) {
            simpleCell *cell = (simpleCell *)[tableView dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
            if(indexPath.row == 0){
                cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
                cell.text.text = @"Send Feedback";
            }
            return cell;
        }

        if (indexPath.section == 2) {
            simpleCell *cell = (simpleCell *)[tableView dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
            if (indexPath.row == 0) {
                cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
                cell.text.text = @"Visit Wowpads.com";
            }

            if (indexPath.row == 1) {

                cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
                cell.text.text = @"Terms of Service";
            }
        }
        return 0;
        }
    }

Upvotes: 1

Views: 1436

Answers (4)

Bac Tran
Bac Tran

Reputation: 3

@NigelG yeah..it worked fine. I added these lines in cellForRowAtIndexPath.

if(indexPath.row == 0){
        [cell.img setImage:[UIImage imageNamed:@"img1.png"]];
    }
    else{
        cell.img.image = nil;
        [cell.img setImage:[UIImage imageNamed:@"img2.png"]];
    }

Upvotes: 0

Rajpal Thakur
Rajpal Thakur

Reputation: 355

Add in CellForRowAtIndexpath

if (cell) {

    cell=nil;
    [cell removeFromSuperview];

}

and also check heightforRowAtIndexpath like -(folat) to -(CGFloat)

Upvotes: 0

houguet pierre
houguet pierre

Reputation: 170

I think you have mistakes on your code, you can set the cell properties on the beginning and after set the values on each case. Try this, hope it will fix your problem.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   simpleCell *cell = (simpleCell *)[self.tableview dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
   if (!cell) {
      cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
   }
   cell.text = @"";
   cell.imageView.image = nil;
   if (indexPath.section == 0) {

      if(indexPath.row == 0){
         if(![[NSUserDefaults standardUserDefaults] boolForKey:@"isFacebookLoggegIn"])  
         {
            cell.imageView.image = [UIImage imageNamed:@"fb_tbl_connect.png"];
         } else {
            cell.imageView.image = [UIImage imageNamed:@"tbl_disconnect_fb.png"];
         }
      }

     if(indexPath.row == 1){
        cell.text.text = @"Edit Your Profile";
     }
  }

  if (indexPath.section == 1) {
      if(indexPath.row == 0){

        cell.text.text = @"Send Feedback";
     }
  }

  if (indexPath.section == 2) {
      if (indexPath.row == 0) {
        cell.text.text = @"Visit Wowpads.com";
      }

      if (indexPath.row == 1) {
        cell.text.text = @"Terms of Service";
     }
  }

return cell;
}

Upvotes: 0

NigelG
NigelG

Reputation: 651

You have the braces mis-aligned. The ending brace for

if (indexPath.section == 0) {

is at the end of the method, not after the end of the code which sets the layout for section 0. This results in the items for sections 1 & 2 not being set correctly.

It is also good practice to set all of the values in the cell, in case the cell is being re-used. I.e. set cell.imageView.image = nil when setting up the cells in sections 1 & 2.

Upvotes: 1

Related Questions