2014-10-17 181 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"]]; 
    }