Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 UITableViewCell中的自定义UIButton-删除不起作用_Iphone_Objective C_Uibutton_Tableview - Fatal编程技术网

Iphone UITableViewCell中的自定义UIButton-删除不起作用

Iphone UITableViewCell中的自定义UIButton-删除不起作用,iphone,objective-c,uibutton,tableview,Iphone,Objective C,Uibutton,Tableview,我在TableViewCell中通过以下方式获得了自定义UIButton: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ident = @"indet"; cell = [tableView dequeueReusableCellWithIdentifier:ident];

我在TableViewCell中通过以下方式获得了自定义UIButton:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ident = @"indet";

    cell = [tableView dequeueReusableCellWithIdentifier:ident];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];

    }

    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame: CGRectMake( 230.0f, 7.5f, 43.0f, 43.0f)];
    [button setImage:[UIImage imageNamed:@"check_bak.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(removeEntry) forControlEvents:UIControlEventTouchUpInside];
    button.tag = [indexPath row];
    [cell addSubview:button];
    cell.textLabel.text =  [myArray objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20.0];
    cell.textLabel.shadowColor = [UIColor whiteColor];
    cell.textLabel.shadowOffset = CGSizeMake(0,1);

    return cell;
}
我在my.h中为removeEntry函数声明了indexPath

removeEntry:

[myArray removeObjectAtIndex:indexPath.row];
[myTable reloadData];
myArray是一个NSMutableArray

这种方法不能正常工作

每次我在indexPath.row删除一个条目时,它都会删除一个条目。但这是错误的。总是有一个条目在上面被删除。即使我使用indexath.row+1/-1

还有别的办法吗?按钮应该留在牢房里


我希望这是可以理解的,对不起,我是德国人。:)

表视图中有多少节?最好将removeEntry方法中的indexPath.row值打印到控制台。如果单击第一行,是否打印出0?第五行,正在打印第4行,等等

编辑:查看您的代码,您将indexpath存储为按钮的标记。在这种情况下,将removeEntry方法更改为如下所示(您可以将返回类型更改为希望返回的任何类型):

添加目标时,在“removeEntry”后添加冒号:

[button addTarget:self action:@selector(removeEntry:) forControlEvents:UIControlEventTouchUpInside];
现在,在removeEntry中,您可以执行以下操作:

UIButton *button = (UIButton *)sender;
[myArray removeObjectAtIndex:button.tag];
[myTable reloadData];

使用下面的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     static NSString *ident = @"indet";

     cell = [tableView dequeueReusableCellWithIdentifier:ident];
     if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];

     }
     if ([cell.contentView subviews]){
        for (UIView *subview in [cell.contentView subviews]) {
                [subview removeFromSuperview];
          }
     }
     //below here your piece of code.
}
原因是,在我们重用单元格的方法中,它保留添加到单元格的所有子视图,并且只刷新单元格的文本部分

希望这对你有用


我在使用UITableView进行iOS编码时遇到了同样的问题。 我通过在cell.contentView上添加按钮并将其从cell.contentView中删除来修复它

添加按钮

[cell.contentView addSubview:button];
而不是[单元格添加子视图:按钮]

在将视图添加到单元格之前,将以下代码行添加到cellForRowIndexPath()方法中,以从单元格中删除按钮

for(UIView *subview in [cell.contentView subviews]){
     [subview removeFromSuperView];
}

它会正常工作。

好吧,它总是不同的,因为如果要添加NSLog(@“%d”,indexath.row),我会计算w/“return[myArray count];”;在removeEntry方法的开头,它应该显示单击位置的索引。首先要确保所单击行的索引是正确的。索引应始终为1减去您单击的行。仍然会删除上面的条目:(尝试打印出button.tag而不是indexPath,并检查值是否正确。从外观上看,它应该可以工作。也许可以用更多代码编辑原始邮件,或者将相关代码粘贴到pastebin上。此外,您确定只使用单个节吗?如何将
indexPath
传递到中de>removeEntry方法以及如何调用它?
for(UIView *subview in [cell.contentView subviews]){
     [subview removeFromSuperView];
}