Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.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
单击单元格IOS上的按钮后删除tableView数据_Ios_Uitableview - Fatal编程技术网

单击单元格IOS上的按钮后删除tableView数据

单击单元格IOS上的按钮后删除tableView数据,ios,uitableview,Ios,Uitableview,我需要删除tableview中的一行以更新更改 我在每个单元格中都有一个删除按钮(tableViewCellController)-请看图片。 单击delete按钮后,UI按钮方法调用tableViewController中的委托方法。delete方法更新数据源(我的模型),我还想更新屏幕(现在该方法重新加载所有数据,但我想从屏幕更新新的change-delete行) 我试着用下面的函数来实现这一点,但是我没有一个发送器(正如我说的,按钮在单元格中按下了,但我实际上在tableView上进行了

我需要删除tableview中的一行以更新更改

我在每个单元格中都有一个删除按钮(
tableViewCellController
)-请看图片。

单击delete按钮后,UI按钮方法调用
tableViewController
中的委托方法。delete方法更新数据源(我的模型),我还想更新屏幕(现在该方法重新加载所有数据,但我想从屏幕更新新的change-delete行)

我试着用下面的函数来实现这一点,但是我没有一个
发送器
(正如我说的,按钮在单元格中按下了,但我实际上在tableView上进行了更改)

功能:

- (IBAction)contactDelete:(id)sender{
    [[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview];

    [self.tableView deleteRowsAtIndexPaths:
        [NSArray arrayWithObject:indexPath]
        withRowAnimation:UITableViewRowAnimationFade];
}
favoritesTableViewCell:

#import "favoritesTableViewCell.h"
#import "ModelUser.h"

@implementation favoritesTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)favoritesDeleteFromFav:(id)sender {
    [[[ModelUser instance] getUser:self.actualLoggedUser] removeFavUser:self.contactUserId];
    [self.delegate onFavDeleteClick];
}
favoritesTableViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.actualLoggedUser  = [NSString stringWithFormat:@"2"];
    [self reloadData];
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadData {

    NSLog(@"Favorites tab was loaded");

    //get id of my favorite contacts
    myFavListId = [[ModelUser instance] getUser:self.actualLoggedUser].contactsFavoriteList;

    //get data of my favorites contacts
    myFavListContactsData = [[NSMutableArray alloc] init];
    for (int i=0; i < [myFavListId count] ; i++) {
        User* us = [[ModelUser instance] getUser:([myFavListId objectAtIndex:i])];
        [myFavListContactsData addObject:us];
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (void) viewDidAppear:(BOOL)animated {
    [self reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return myFavListContactsData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    favoritesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"favoriteCell" forIndexPath:indexPath];

    User *us = [myFavListContactsData objectAtIndex:indexPath.row];

    //setting cell data
    cell.actualLoggedUser = self.actualLoggedUser;
    cell.contactUserId = us.userId;
    cell.contactName.text = [NSString stringWithFormat:@"%@ %@",us.fname,us.lname];
    [cell.contactImage setImage: [UIImage imageNamed:us.imageName]];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    User *us = [myFavListContactsData objectAtIndex:indexPath.row];

    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    userDetailsProfile*  udVC = [sb
                                 instantiateViewControllerWithIdentifier:@"userDetailsProfile"];
    udVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    udVC.userDetailId = [NSString stringWithFormat:@"%@", us.userId];
    [self showViewController:udVC sender:self];
}

- (void)onFavDeleteClick {
    [self reloadData];
}

@end
-(void)viewDidLoad{
[超级视图下载];
self.actualLoggedUser=[NSString stringWithFormat:@“2”];
[自重新加载数据];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(void)重新加载数据{
NSLog(@“收藏夹选项卡已加载”);
//获取我最喜欢的联系人的id
myFavListId=[[ModelUser实例]getUser:self.ActualLogedUser].contactsFavoriteList;
//获取我的收藏夹联系人的数据
myFavListContactsData=[[NSMutableArray alloc]init];
对于(int i=0;i<[myFavListId计数];i++){
User*us=[[ModelUser实例]getUser:([myFavListId objectAtIndex:i]);
[myFavListContactsData添加对象:美国];
}
[self.tableView重载数据];
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//返回节数。
返回1;
}
-(无效)视图显示:(BOOL)动画{
[自重新加载数据];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//返回节中的行数。
返回myFavListContactsData.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
favoritesTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“favoriteCell”forIndexPath:indexPath];
User*us=[myfavlistcontactsdataobjectatindex:indexPath.row];
//设置单元格数据
cell.actualLoggedUser=self.actualLoggedUser;
cell.contactUserId=us.userId;
cell.contactName.text=[NSString stringWithFormat:@“%@%@”,us.fname,us.lname];
[cell.contactImage setImage:[UIImage imageName:us.imageName]];
cell.delegate=self;
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
User*us=[myfavlistcontactsdataobjectatindex:indexPath.row];
UIStoryboard*sb=[UIStoryboard故事板,名称:@“Main”bundle:nil];
userDetailsProfile*udVC=[sb
实例化EviewController标识符:@“userDetailsProfile”];
udVC.modaltransationstyle=uimodaltransationstyle;
udVC.userDetailId=[NSString stringWithFormat:@“%@”,us.userId];
[自显示视图控制器:udVC发送者:自];
}
-(无效)在FAVDelete上单击{
[自重新加载数据];
}
@结束

您需要更新数据源并删除从cell类调用的委托回调中的单元格(
onFavDeleteClick
favoritesTableViewCell的委托方法,在您的情况下)

这个过程应该是这样的:

-(IBAction)favoritesDeleteFromFav:(id)sender {
    [self.delegate onFavDeleteClick:self];
}
-(void)onFavDeleteClick:(favoritesTableViewCell*)cell {
    //update model
    [[[ModelUser instance] getUser:self.actualLoggedUser]  removeFavUser:self.contactUserId];

    //update table view
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if (indexPath) {
        [self.tableView deleteRowsAtIndexPaths:@[indexPath]
        withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
  • 在“favoritesTableViewCell.h”中,声明一个包含
    onFavDeleteClick
    方法的协议。我想你已经完成了这一步。您需要做的是在FAVDeleteClick:(favoritesTableViewCell*)单元格
  • 上将方法签名更新为
    -(void)
    
  • 从“favoritesTableViewCell.m”调用
    favoritesDeleteFromFav
    方法如下:

    -(IBAction)favoritesDeleteFromFav:(id)sender {
        [self.delegate onFavDeleteClick:self];
    }
    
    -(void)onFavDeleteClick:(favoritesTableViewCell*)cell {
        //update model
        [[[ModelUser instance] getUser:self.actualLoggedUser]  removeFavUser:self.contactUserId];
    
        //update table view
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        if (indexPath) {
            [self.tableView deleteRowsAtIndexPaths:@[indexPath]
            withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    
  • 现在,在主
    UITableView
    存在的视图控制器中,实现如下回调方法:

    -(IBAction)favoritesDeleteFromFav:(id)sender {
        [self.delegate onFavDeleteClick:self];
    }
    
    -(void)onFavDeleteClick:(favoritesTableViewCell*)cell {
        //update model
        [[[ModelUser instance] getUser:self.actualLoggedUser]  removeFavUser:self.contactUserId];
    
        //update table view
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        if (indexPath) {
            [self.tableView deleteRowsAtIndexPaths:@[indexPath]
            withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    

  • 这就是你需要做的一切来达到你想要的效果。

    这正是我需要的。但我得到错误:在-(void)onFavDeleteClick:(favoritesTableViewCell*)单元格上应为类型@AyanI试图将其更改为:-(void)onFavDeleteClick:(UITableViewCell*)cell contactUserId:(NSString*)contactUserId;但我得到了以下错误:异常“NSInternalInconsistencyException”,原因:“无效更新:节0中的行数无效。更新(3)后现有节中包含的行数必须等于更新(3)前该节中包含的行数,加上或减去从该节中插入或删除的行数(0插入,1删除),加上或减去移入或移出该节的行数(0移入,0移出)“@jdoe非常抱歉,我最近不能参加。根据经验,您的
    onFavDeleteClick
    方法应该执行以下操作:1。从tableview数据源中删除特定索引处的项。2.使用
    deleteRowsAtIndexPaths:withRowAnimation
    方法从tableview中删除该索引处的单元格。您有责任确保在方法完成后,tableview单元格的数量==tableview数据源中的项数。如果不这样做,将调用异常,您已经提到过。因此,在从tableview中删除单元格之前,请正确更新数据源以匹配计数。我编辑了这个问题,因为它不是用这里首选的技术写作。我想,关于这一点,社区会进行几次讨论。问题是。如果您不确定某个编辑是否符合社区标准,最好询问编辑,以便他们能给您提供建议。