2014-10-17 136 views
1

我有以下代码创建我的应用程序的设置视图。但是当我滚动时,底部部分被第1部分(包含图像的一个单元格)取代,有什么建议?我尝试了很多东西(特别是dequeueReusableCellWithIdentifier),但都没有成功。ios - UITableView重复单元格

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

回答

1

您的大括号不一致。对于

if (indexPath.section == 0) { 

结束支架是在该方法结束时,而不是代码的端部设置为0部分的布局这导致物品部分1 & 2没有被正确设定之后。

如果单元格被重新使用,设置单元格中的所有值也是一种很好的做法。即设置cell.imageView.image = nil设置部分中的单元格1 & 2.

+0

这是一个错误,我已纠正它。但我的代码仍然不起作用 – Yrol 2014-10-17 09:20:43

+0

你使用heightforrowAtindexpath吗? – 2014-10-17 09:57:38

+0

http://stackoverflow.com/questions/26398924/tableview-sections-overwrite-in-iphone5s-and-greater-versions-只检查它 – 2014-10-17 09:59:59

0

我认为你的代码存在错误,你可以在开始时设置单元格属性,并在设置每个案例的值后设置单元格属性。试试这个,希望它能解决你的问题。

- (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; 
} 
+0

我会给它一个去,谢谢 – Yrol 2014-10-17 09:27:42

+0

仍然得到重复单元格。 – Yrol 2014-10-20 12:12:56

0

添加中的cellForRowAtIndexPath

如果(细胞){

cell=nil; 
    [cell removeFromSuperview]; 

}

,并检查heightforRowAtIndexpath像 - (folat)到 - (CGFloat的)

0

@NigelG 是啊..它工作得很好。 我在cellForRowAtIndexPath中添加了这些行。

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