天天看點

iOS - *** Terminating app due to uncaught exception 'NSRangeException&am

* Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘ -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]’ ** First throw call stack: (0x3693b2a3 0x3443b97f 0x36886b75 0x18e391 0x36367275 0x363e9ea9 0x38a09a6f 0x369105df 0x36910291 0x3690ef01 0x36881ebd 0x36881d49 0x345372eb 0x362f9301 0x95d55 0x95c50) libc++abi.dylib: terminate called throwing an exception

在一個項目裡,UI部分是UITableView,經常出現上述錯誤,一般都是由于 :indexPath.row查找不到資料導緻的問題

解決辦法:

舉例說明:具體解決辦法看需求

如下需求是:請求資料最多不超多三條

如果是直接:return 3 ; 便會出現如上述錯誤,由于第一次視圖在布局的時候資料還未請求傳回的:ListArray.count = 0;得到資料後為:ListArray.count。

  • 是以首先需要判斷是否傳回資料為零條,或者小于三條。
  • 若是有多少條資料傳回多少直接:return ListArray.count ;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else{
        if (_ListArray.count != 0) {

          if (_ListArray.count > 3){
            return 3;
          }else{

                return  _ListArray.count;
          }
        }else {
            return 0;

        }
    }
}