【问题标题】:Button in UITableView is appearing twice in iPad Master View ControllerUITableView 中的按钮在 iPad 主视图控制器中出现两次
【发布时间】:2013-12-25 11:41:23
【问题描述】:

以编程方式创建了一个 UITableView,我在 iPad 主视图控制器中的 tableview 中的数据是动态加载的,我在最后一个单元格中放置了一个按钮,但该按钮再次出现在单元格下方的 tableview 中...第一次代码执行后,按钮只出现一次,但下一次出现两次...这是代码...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if([_objects count] > 1)
{

    if(indexPath.row == [_objects count])
    {            

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        //set the position of the button
        button.frame = CGRectMake(cell.frame.origin.x + 50, cell.frame.origin.y + 15, 225, 30);
        [button setTitle:@"Hide me" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(customActionPressed) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor= [UIColor clearColor];
        [cell.contentView addSubview:button];

    }
    else
    {
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.textColor = [UIColor colorWithRed:0/255.0 green:100.0/255.0 blue:220.0/255.0 alpha:1.0];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        cell.textLabel.text = [_objects objectAtIndex:indexPath.row];
        NSLog(@"%@", [_objects objectAtIndex:indexPath.row]);
    }
}

return cell;
}

for ex:: 这是 tableview 的快照(我怎么能把图像放在这里???)

  1. aaa
  2. bbb
  3. ccc
  4. ddd
  5. 哎呀
  6. UI按钮

    UIButton 再次出现在 tableview 中

    这是图片链接http://s10.postimg.org/fwarb2661/i_OS_Simulator_Screen_shot_28_Dec_2013_10_57_32_A.png

    任何帮助表示赞赏,在此先感谢..

【问题讨论】:

  • 请在此处发布一些代码
  • 谢谢,上面添加了一些代码

标签: iphone objective-c ipad uitableview ios6


【解决方案1】:

请写下面的代码:

 if (cell)

   {

     cell =nil;

   }

在创建表格视图单元之前

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//写在这里

if (cell)

   {

     cell =nil;

   }

//*

if (cell == nil) {

   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }

【讨论】:

  • 伙计,它工作得很好......但是为什么单元格重复,你能告诉我吗???感谢您的解决方案:)
  • @grk 请与我分享您的屏幕截图,然后我可以检查您的问题。
  • 这里是图片链接s10.postimg.org/fwarb2661/…
【解决方案2】:

要在单元格中设置背景图像,请使用以下代码:-

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(X, Y, 277, 58)];

imageView.backgroundColor = [UIColor clearColor];

imageView.opaque = NO;

imageView.image = [UIImage imageNamed:@"yourImage.png"];

cell.backgroundView = imageView;

【讨论】:

    【解决方案3】:

    可能是因为您的表格视图再次使用相同的单元格。 确保每次都从单元格中删除按钮。

    有助于查看您的代码,以便我可以指出您的确切位置...

    【讨论】:

      【解决方案4】:

      试试这个:

      (void)prepareForReuse {
          [super prepareForReuse];
      }
      

      在方法中将按钮设置为nil 或任何其他显示两次的元素。该单元格正在被重复使用,您需要运行 prepareForReuse 以清空旧单元格。

      【讨论】:

        猜你喜欢
        • 2015-12-02
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多