UITableView cellForRowAtIndexPath called more than once for each row

前端 未结 3 557
青春惊慌失措
青春惊慌失措 2021-01-18 05:54

If I have a UITableView that has 4 rows in it then shouldn\'t the cellForRowAtIndexPath method be called 4 times when I navigate to that view? I am finding that it is being

相关标签:
3条回答
  • 2021-01-18 06:32

    Highly probable that you are calling from other methods :

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    

    self.tableView from outside of - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath will call cellForRowAtIndexPath twice.

    0 讨论(0)
  • 2021-01-18 06:41

    The method is called when the cell appears, so whenever a cell appears, the method is called. Also, the cells are reused. So even if the method for a particular cell is called once, it maybe called for another time when this cell disappears and then appears again.

    0 讨论(0)
  • 2021-01-18 06:50

    Swift

    DispatchQueue.main.async {
       //Add selection in main queue
    }
    

    This will fix the issue.

    0 讨论(0)
提交回复
热议问题