How to use Reusable Cells in uitableview for IOS

后端 未结 6 1537
日久生厌
日久生厌 2020-12-09 05:57

Ok. I cant seem to get a firm understanding on how tableviews work. Would someone please explain to me how cells are reused in tableviews especially when scrolling? One of t

6条回答
  •  抹茶落季
    2020-12-09 06:38

    I need to see your code, but i can say u didn't change your model or you changed it all of rows in your array.

    You can use some class extended from UITableViewCell and in "cellForRowAtIndexPath" method use it as reusable cell.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
       MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    
    
        // Configure the cell...
        MyModel *model = [_models objectAtIndex:indexPath.row];
        cell.nameLabel.text = model.name;
        cell.image.image = model.image;
        cell.likeButton.text = model.likeButtonText;
        return cell;
     } 
    

    and then in your "didSelectRowAtIndexPath" method change "model.likeButtonText".

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
       MyCell cell* =     [tableView cellForRowAtIndexPath:indexPath];
       MyModel *model = [_models objectAtIndex:indexPath.row];
       model.likeButtonText = [model.likeButtonText isEqual:@"like"]?@"unlike":@like;
       cell.likeButton.text = model.likeButtonText;
    }
    

    This will update your cell

提交回复
热议问题