iPhone開發-ios7環境下Uitableview刪除某一行後忽略點擊事件

來源:互聯網
上載者:User

下面代碼的作用是:當你點擊選中tableview的某一行時,它將記錄被選中的行。當你左掃並刪除某一行時,它將刪除該行資料並更新Uitableview中的資料。

@interface DummyTableViewController : UITableViewController@property (nonatomic, strong) NSMutableArray *items;@end@implementation DummyTableViewController- (instancetype)initWithStyle:(UITableViewStyle)style{    self = [super initWithStyle:style];    if (self)    {        _items = [ @[ @"A", @"B", @"C", @"D", @"E" ] mutableCopy];    }    return self;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.items count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];    cell.textLabel.text = self.items[indexPath.row];    return cell;}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete)    {        [self.items removeObjectAtIndex:indexPath.row];        [tableView reloadData];    }}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"Row %@ tapped.", self.items[indexPath.row]);}

在ios6環境下,上段代碼按照剛才描述的預期工作。但是在ios7環境下,我做了如下操作:當tableview中的一行被刪除並更新tablview後,被刪除行的下一行的點擊事件將被忽略,導致點擊該行無反應。很奇怪是不是,下面將解釋原因。

當tableview中的某行被選中刪除後,tableview將處於編輯(editing)狀態,所以你需要將tableview中的狀態更換成選擇(selection)模式,更改代碼如下:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{  if (editingStyle == UITableViewCellEditingStyleDelete)  {    [self.items removeObjectAtIndex:indexPath.row];    // Turn off editing state here    tableView.editing = NO;    [tableView reloadData];  }}


註:本人水平有限,翻譯錯誤在所難免,如有錯誤望指正,在下會儘快修改。


原文:http://stackoverflow.com/questions/19364409/uitableviewcontroller-ignores-tap-after-deleting-row-in-ios7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.