objc
if(indexPath.row == self.items.count-1) { cell = [tableView dequeueReusableCellWithIdentifier:QuestionCellIdentifier forIndexPath:indexPath]; } else { cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath]; } return cell;
和
objc
cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath]; if(indexPath.row == self.items.count-1) { cell = [tableView dequeueReusableCellWithIdentifier:QuestionCellIdentifier forIndexPath:indexPath]; } return cell;
我认为这两种写法结果上应该没有区别,但在我的项目中,返回的结果却是不同的,第一种写法,返回了正确的结果,第二种写法,进入了判断语句中,然后返回了QuestionCell,但是在显示上,却显示的是CommentCell,然后点击,又刷新成了QuestionCell,我确认是在主线程中执行的。
请问为什么会是这种结果?我猜测是dequeueReusableCellWithIdentifier:indexPath
中进行了预加载,是这样么?
问题可能隐藏在cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];与index.row和self.items.count的调用顺序,因为你调用的时候不能保证它们只是单纯返回值,而没有进行某些修改操作。
可以试试在方法开头先把index.row和self.items.count赋值给局部变量。