Increase the main tableview row height according to the custom cell

前端 未结 7 639
名媛妹妹
名媛妹妹 2021-01-03 07:32

I am having an app in which I have a Tableview and on that tableview\'s each row I am dynamically creating a custom tableview cell.

Below is the cod

7条回答
  •  离开以前
    2021-01-03 07:38

    Here we go:

    1. Create a common UITableViewCell subclass for all the cells in your view (such as FlowViewAbstractTableViewCell)
    2. Create a class method on the table view cell subclass such as + (CGFloat)sizeForCellWithComment:(Comment *)comment. What you're doing is setting up a method to pass a model object into that each table view cell subclass can implement.
    3. Create however many other cell subclasses you need that inherit from FlowViewAbstractTableViewCell. In your case, it looks like you could have ones called FlowViewTextCommentTableViewCell and FlowViewPictureCommentTableViewCell.
    4. For each of those subclasses, implement that +sizeForCellWithComment: method. This will allow you to return a variable size based on the object you pass into the method.
    5. Implement - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath for your UITableViewDelegate. In this method you are going to figure out which cell class you want to use and which object to pass into that size method. For example:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { Comment *commentForCell = [self.comments objectAtIndex:indexPath.row]; return [CommentTableViewCell sizeForComment:commentForCell].height; }

    This will allow you to return a variable cell height based on the object. I use variably sized table view cells all the time & love this pattern because I think it does the best job of hiding the implementation and following MVC.

提交回复
热议问题