0
我在UItableView中显示评论并在单元格中创建删除按钮。当我删除任何评论时它将被删除。我用[Tableview reloadData]
,但它总是从表中删除最后一个单元格,当我删除评论下一次它是好的。为什么表视图始终删除最后一个单元格选中。我的代码是uitableview单元格中的删除按钮无法正常工作
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//delete button in uitableview cell ================================
deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(270, 10, 20, 20);
//[deleteBtn setTitle:@"delete" forState:UIControlStateNormal];
[deleteBtn setImage:[UIImage imageNamed:@"deletefb.png"] forState:UIControlStateNormal];
deleteBtn.tag = indexPath.row;
[deleteBtn addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
deleteBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[cell.contentView addSubview:deleteBtn];
}
return cell;
}
删除方法是
- (void)delete:(id)sender {
UIButton *myDeleteButton = (UIButton *)sender ;
//delete method of comment
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:1];
[variables setObject:@"delete" forKey:@"method"];
[fbGraph doGraphPost:[NSString stringWithFormat:@"%@",[(Facebook *)[tableArray objectAtIndex:myDeleteButton.tag]postId]] withPostVars:variables];
//load tableview
[self responseMethod]; //method to load comments
//show alert
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Deleted"
message:@"" delegate:nil
cancelButtonTitle:@"Ok" otherButtonTitles:nil ];
[alert show];
[alert release];
}
我用deleteBtn.tag = indexPath.row之外,但仍是删除最后一行 – iProgrammer
的NSString *标识符= @ “someUniqueValue”; 我也使用过这个..但是我的表格单元格的值将移动到第一个单元格并互相交换 – iProgrammer
您确定它实际上是*删除*最后一行,或者因为您重新查询现在有一个较少结果的数据源tableview只显示少一个记录?这会造成删除最后一行时的错觉,但事实并非如此。要确定究竟发生了什么是有点困难的,因为你似乎没有以典型的方式去除单元格。当我删除任何单元格时,实际上都是 –