天天看點

UItableView 點選cell跳轉到其它界面延遲bug第一個方法。                                                                                                                                                                   

最近在做項目等時候遇見一個bug,點選UITableView的Cell跳轉到其他界面,經常會有延時,有時四五秒,有時會有十幾秒。

檢視網上也沒有什麼詳細的介紹。iOS群裡問也沒有問出正确的答案。最後讓自己找出了答案。

我用的裝置iPhone5s,iOS版本8.4。模拟器測試也有問題。

我的第一個界面中的相關代碼。其他的一些沒什麼關系的代碼就不寫了。

//傳回每一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
            reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    GASettingUtils *set = _tableSettingAry[indexPath.row];
    cell.textLabel.text = set.settingTitle;
    cell.detailTextLabel.text = set.settingDetail;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
//點選事件。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row ==0) {
          NSLog(@"click tableview");
         //跳轉到第二個界面
         [self performSegueWithIdentifier:@"showSettingHotSpot" sender: self]; 
     }
}
           

第二個界面,并沒有什麼耗時操作(網絡啊。。。),也沒有太多控件,隻是一個分組樣式的UITableVIew。

解決辦法找到兩個。

第一個方法。                                                                                                                                                                   

将cell.selectionStyle設定成1、2、3。

也就是不要這樣:cell.selectionStyle =UITableViewCellSelectionStyleNone;

設定成這幾種都可以:

cell.selectionStyle = UITableViewCellSelectionStyleBlue; // 1

cell.selectionStyle = UITableViewCellSelectionStyleGray;  //2

cell.selectionStyle = UITableViewCellSelectionStyleDefault;  //3 

這樣後沒有出現那個問題。

第二個方法                                                                             

不管cell的selectionStyle,在tableview的點選事件方法中,也就是 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
           

在這個方法中添加一句代碼

[tableView deselectRowAtIndexPath:indexPath animated:YES];

這樣不論cell的selectionStyle是什麼。都沒有在延遲。

之後又發現一個問題,就是點選cell彈出alertview的時候,也會出現延遲。

這個經多次測試隻能使用第一個方法避免這種問題的出現,第二種不能解決。