我需要在tableview中删除一行来更新我的更改。
我在每个单元格中都有一个删除按钮(tableViewCellController
) - 查看图片。
Screenshot
单击删除按钮后,UI按钮方法将调用tableViewController
中的委托方法。删除方法更新数据源(我的模型),我也想更新屏幕(现在方法重新加载所有数据,但我想更新新的更改 - 从屏幕删除行。)
我尝试使用以下功能执行此操作,但我没有sender
(正如我所说按钮在单元格中按下,但我实际上在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
答案 0 :(得分:0)
您需要更新数据源并删除从您的单元格类调用的委托回调中的单元格(在您的情况下为onFavDeleteClick
类的favoritesTableViewCell
委托方法)。
这个过程应该是这样的:
onFavDeleteClick
方法的协议。我想你已经完成了这一步。您需要做的是将方法签名更新为-(void) onFavDeleteClick:(favoritesTableViewCell*)cell
。从“favoritesTableViewCell.m”调用favoritesDeleteFromFav
这样的方法:
-(IBAction)favoritesDeleteFromFav:(id)sender {
[self.delegate onFavDeleteClick:self];
}
现在,在主UITableView
存在的视图控制器中,实现如下的回调方法:
-(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];
}
}
这就是你需要做的一切,以达到你想要的效果。 希望能帮助到你。快乐的编码!!