我想实施"轻扫以删除" tableview的选项。我添加了" commitEditingStyle"方法,它触发滑动和一个红色框与"删除"显示,但我无法解决该方法中应该包含的内容。有帮助吗? 这是代码:
#import "NotificationTableView.h"
#import "NotificationTableViewCell.h"
@interface NotificationTableView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableDictionary* heights;
@end
@implementation NotificationTableView
-(void)awakeFromNib{
UINib *nib = [UINib nibWithNibName:@"NotificationTableViewCell" bundle:nil];
[self registerNib:nib forCellReuseIdentifier:@"NotificationTableViewCell"];
[self registerNib:nib forCellReuseIdentifier:@"NotificationTableViewCellSize"];
self.dataSource = self;
self.delegate = self;
self.estimatedRowHeight = 86;
self.rowHeight =
// (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) ? UITableViewAutomaticDimension :
86;
self.tableFooterView = [UIView new];
_heights = [NSMutableDictionary new];
}
-(void)setNotifications:(NSArray *)notifications{
_notifications = notifications;
[self reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
[self reloadData];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return self.notifications.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationTableViewCell* cell = [self dequeueReusableCellWithIdentifier:@"NotificationTableViewCell"];
[cell configureCellWithNotification:self.notifications[indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Notification* not = self.notifications[indexPath.row];
[self.notificationDelegate notificationTapped:not];
}
@end
答案 0 :(得分:1)
reloadData
外,请勿致电deleteRowsAtIndexPaths:
。做一个。self.notifications
数组中删除相应的行。这意味着您需要一个可变数组。beginUpdate
/ endUpdate
,因为您只需拨打一次电话即可修改表格视图。答案 1 :(得分:0)
试试这个:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Get the notification here
Notification* not = self.notifications[indexPath.row];
//Remove item in array
[self.notifications removeObjectIdenticalTo:not];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}