Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableView中的按钮在iPad主视图控制器中出现两次_Iphone_Objective C_Ipad_Uitableview_Ios6 - Fatal编程技术网

Iphone UITableView中的按钮在iPad主视图控制器中出现两次

Iphone UITableView中的按钮在iPad主视图控制器中出现两次,iphone,objective-c,ipad,uitableview,ios6,Iphone,Objective C,Ipad,Uitableview,Ios6,以编程方式创建了UITableView,iPad主视图控制器中我的tableview中的数据是动态加载的,我在最后一个单元格中放置了一个按钮,但该按钮再次显示在单元格下方的tableview中。。。第一次执行代码时,按钮只显示一次,但下次它显示两次。。。这是代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSStrin

以编程方式创建了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;
}
例如:这里是tableview的快照(如何将图像放在这里?)

  • aaa
  • bbb
  • ccc
  • ddd
  • eee
  • UIButton

    UIButton再次出现在tableview中

    这里是图像链接

    如有任何帮助,我们将不胜感激


  • 对于单元格中的背景图像设置,请使用以下代码:-

    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;
    

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


    请查看您的代码,以便我能指出您的确切位置…

    请填写以下代码:

     if (cell)
    
       {
    
         cell =nil;
    
       }
    
    在创建tableview单元格之前

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    //写在这里

    if (cell)
    
       {
    
         cell =nil;
    
       }
    
    //*

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

    (void)prepareForReuse {
        [super prepareForReuse];
    }
    

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

    请在此发布一些代码谢谢,在上面添加了一些代码,它工作正常。。。但是为什么手机会重复,你能告诉我吗???谢谢您的解决方案:)@grk请与我分享您的截图,然后我可以检查您的问题。这是图像链接