Assertion failure when using UISearchDisplayController in UITableViewController

后端 未结 7 2051
一个人的身影
一个人的身影 2020-12-07 12:55

I\'ve been trying to add simple Search functionality to a TableViewController in my app. I followed Ray Wenderlich\'s tutorial. I have a tableView with some data, I added th

7条回答
  •  春和景丽
    2020-12-07 13:49

    Dequeue the cell without using the 'indexPath' and in case of you obtain a nil element, you have to allocate it manually.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YourCellId"];
        if (!cell)
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YourCellId"];
    
        // fill your cell object with useful stuff :)
    
        return cell;
    }
    

    Trying to use self.tableView for dequeue the cell may cause crashes when you have a sectioned main list and a plain search list. This code instead work in any situation.

提交回复
热议问题