ios - UITableView重复单元格

时间:2014-10-17 08:50:23

标签: ios uitableview

我有以下代码,可以创建我的应用的设置视图。但是当我滚动时,底部部分被第1部分(包含图像的一个单元格)替换,任何建议?我尝试了很多东西(特别是dequeueReusableCellWithIdentifier),但都没有用。

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

4 个答案:

答案 0 :(得分:1)

你的大括号错了。

的结束括号
if (indexPath.section == 0) {

在方法的最后,而不是在为第0部分设置布局的代码结束之后。这导致第1和第1部分的项目。 2未正确设置。

如果要重复使用单元格,最好在单元格中设置所有值。即在第1和第1部分中设置单元格时设置cell.imageView.image = nil 2。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

添加CellForRowAtIndexpath

if(cell){

    cell=nil;
    [cell removeFromSuperview];

}

并检查heightforRowAtIndexpath之类的 - (folat)to - (CGFloat)

答案 3 :(得分: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"]];
    }