Ios 从UITableView中删除自定义行

Ios 从UITableView中删除自定义行,ios,objective-c,uitableview,protocols,Ios,Objective C,Uitableview,Protocols,我只是在用UITableView练习。 以下是我到目前为止所做的 从头开始使用xib.file创建单元格 数据存储在P-list中 使用[MutableCopy]并将数组转换为mutableArray 看起来有点奇怪,但我使用故事板建立了代理连接,因此这里没有“self.tableView.delegate=self”行 现在的问题是我可以删除mutableArray中的对象,但不能在UI上删除。我选择的行保留在表上。 我知道“-(void)tableView:(UITableView*)t

我只是在用UITableView练习。 以下是我到目前为止所做的

  • 从头开始使用xib.file创建单元格
  • 数据存储在P-list中
  • 使用
    [MutableCopy]
    并将数组转换为
    mutableArray
  • 看起来有点奇怪,但我使用故事板建立了代理连接,因此这里没有“
    self.tableView.delegate=self
    ”行
现在的问题是我可以删除
mutableArray
中的对象,但不能在UI上删除。我选择的行保留在表上。 我知道“
-(void)tableView:(UITableView*)tableView committedingStyle:(UITableViewCellEditingStyle)rowatindexpath的editingStyle:(NSIndexPath*)indexPath
”委托方法有效,但出现了一些问题

任何建议都将不胜感激!谢谢:)

![#导入“ViewController.h”
#导入“Model.h”
#导入“SimpleTableCell.h”
@界面视图控制器()
{
NSMUTABLEARRY*MC;
NSMutableArray*_imagesMC;
模型*\u模型;
}
@结束
@实现视图控制器
-(无效)viewDidLoad
{
\[超级视图下载\];
//加载视图后,通常从nib执行任何其他设置。
模型*Model=\[\[Model alloc\]init\];
\[模型返回\];
NSArray*配方=model.recipes;
NSArray*images=model.imagesArray;
_recipesMC=\[recipes mutableCopy\];
_imagesMC=\[images mutableCopy\];
}
-(无效)未收到记忆警告
{
\[super didReceiveMemoryWarning\];
//处置所有可以重新创建的资源。
}
#布拉格马克·德莱加特·帕特恩
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回\[\u mc count\];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString*cellIdentifier=@“SimpleTableCell”;
SimpleTableCell*单元格=(SimpleTableCell*)\[tableView dequeueReusableCellWithIdentifier:cellIdentifier\];
如果(单元格==nil)
{
NSArray*nib=\[\[NSBundle mainBundle\]loadNibNamed:@“空”所有者:自选项:nil\];
单元格=\[nib对象索引:0\];
}
cell.namelab.text=\[\u recipesMC objectAtIndex:indexath.row\];
如果(indexPath.row<\[\u recipesMC count\]-1)
{
UIImage*image=\[UIImage ImageName:[U imagesMC\[indexPath.row\]\];
cell.thumbnailImageView.image=图像;
}
返回单元;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
返回78;
}
#pragma标记代表触摸
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
NSLog(@“%li”,(长)indexath.row);
UIAlertView*alert=\[\[UIAlertView alloc\]initWithTitle:@“嘿”消息:\[NSString stringWithFormat:@“%@”,\u recipesMC\[indexPath.row\]\]委托:自取消按钮:@“确定”其他按钮:nil\];
\[警报显示\];
UITableViewCell*cell=\[tableView cellForRowAtIndexPath:indexPath\];
if(cell.accessoryType=
UITableViewCellAccessorOne){
cell.accessoryType=
UITableViewCellAccessorOne;
}
其他的
{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
\[self-PerformsgueWithIdentifier:@“CellSelectionSegue”发件人:self\];
}
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
NSLog(@“单击了%i处的删除按钮”,indexath.row);
\[\u recipesMC removeObjectAtIndex:indexPath.row\];
\[\u imagesMC removeObjectAtIndex:indexath.row\];
\[self.tableView重新加载数据\];
NSLog(@“count=%i”,\[\u mc count\]);
}
#布拉格马克赛格
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
NSLog(@“Segue”);
}
@结束

Yo应该调用
deleterowsatindepath:wothRowAnimation:

e、 g.在我的代码中:

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ElKeyValuePair *pair = [self.locationFavoritesKeyArr objectAtIndex:[indexPath row]];
        [self.locationFavoritesKeyArr removeObjectAtIndex:[indexPath row]];
        ELCarpark *carpark = [self.agglomeration carparkForUid:[pair.val1 intValue]];
        [carpark removeFromFavoritesAgglomerationUid:self.agglomeration.uid];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

将此代码添加到方法
tableView:CommittedItingStyle:forRowAtIndexPath
应将该行从数据源和UI中删除

if (editingStyle == UITableViewCellEditingStyleDelete) { [_recipesMC removeObjectAtIndex:indexPath.row]; [_imagesMC removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:YES]; } 如果(editingStyle==UITableViewCellEditingStyleDelete) { [\u recipesMC removeObjectAtIndex:indexath.row]; [\u imagesMC removeObjectAtIndex:indexath.row]; [tableView deleteRowsAtIndexPaths:indexPath with RowAnimation:YES]; }
最后删除
reloadData
指令。

我很确定您的
self.tableView
nil

这是因为即使您将self设置为数据源和委托,您的表也会工作,但在这里,您似乎已经创建了
UITableView*tableView
,但没有通过界面链接它(如果您使用构建器)

或者,您可以简单地调用
tableView
而不是
self.tableView
,因为委托协议为您提供了表

只要这样做,它就会起作用。然后,你可以尝试Szu的解决方案

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"Clicked DELETE BUTTON at %i",indexPath.row);

    [_recipesMC removeObjectAtIndex:indexPath.row];
    [_imagesMC removeObjectAtIndex:indexPath.row];

    [tableView reloadData];

    NSLog(@"count = %i",[_recipesMC count]);

}

你确定
self.tableView
不是
nil
?因为他调用了
reloadData
,所以不需要它。似乎他的
reloadData
不起作用。是的。我只是不明白为什么reloadData不起作用。它知道这个方法应该起作用,但当时tableView没有从plist和di检索数据dn不使用xib.file修改单元格。没问题:-)您应该避免在可能的情况下调用reloadData。reloadData需要花费很多时间,而且也不会显示一些行动画。谢谢!我错过了将self.tableView连接到接口生成器的机会。这两个功能都很好。我非常感谢您的帮助。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"Clicked DELETE BUTTON at %i",indexPath.row);

    [_recipesMC removeObjectAtIndex:indexPath.row];
    [_imagesMC removeObjectAtIndex:indexPath.row];

    [tableView reloadData];

    NSLog(@"count = %i",[_recipesMC count]);

}