how to obtain the UITableViewCell within heightForRowAtIndexPath?

后端 未结 6 661
孤城傲影
孤城傲影 2020-12-24 12:19

How does one obtain the UITableViewCell when within the heightForRowAtIndexPath method, i.e. given the indexPath?

(then I could access the

相关标签:
6条回答
  • 2020-12-24 12:58

    This question has no sense because in

    heightForRowAtIndexPath
    

    no cells are created yet. Here's how tableView works:

    1. TableView asks it's datasource how many sections it will have. -numberOfSectionsInTableView
    2. Asks it's datasource how many rows each section will have (to know how big scroller should be etc.) -numberOfRowsInSection
    3. Asks it's delegate height of each visible row. To know where cells will be located. - heightForRowAtIndexPath
    4. Lastly it asks datasource to give it a cell to display at given index path -cellForRowAtIndexPath

    So you can't access to cells from heightForRowAtIndexPath because with a most likely cell is not created yet.


    However in case I misunderstood your question go try:

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    0 讨论(0)
  • 2020-12-24 13:05

    i would suggest you to calculate the correct height of a row in table:heightForRowAtIndexPath: by using your data structure (Height of text,images,mutliline text etc..).

    And change the component height in their -(void) layoutSubviews method.

    0 讨论(0)
  • 2020-12-24 13:14

    The obvious answer is to call cellForRowAtIndexPath, but you may have already discovered there are some issues with that. See this question if you haven't already: UITableView flexible/dynamic heightForRowAtIndexPath

    For my last project I used a custom subclass of UICell and implemented a method like this. I then called this from table:heightForRowAtIndexPath: (after looking up the content for that row).

    + (CGFloat) heightOfContent: (NSString *)content
    {
        CGFloat contentHeight = 
               [content sizeWithFont: DefaultContentLabelFont
                   constrainedToSize: CGSizeMake( DefaultCellSize.width, DefaultContentLabelHeight * DefaultContentLabelNumberOfLines )
                       lineBreakMode: UILineBreakModeWordWrap].height;
        return contentHeight + DefaultCellPaddingHeight;
    }
    
    0 讨论(0)
  • 2020-12-24 13:16

    For iOS8:

    override func viewDidLoad() {
            super.viewDidLoad()
    
            self.tableView.estimatedRowHeight = 80
            self.tableView.rowHeight = UITableViewAutomaticDimension
        }
    

    OR

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
                return UITableViewAutomaticDimension
    
        }
    

    But for iOS7, the key is calculate the height after autolayout,

    func calculateHeightForConfiguredSizingCell(cell: GSTableViewCell) -> CGFloat {
            cell.setNeedsLayout()
            cell.layoutIfNeeded()
            let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height + 1.0
            return height
    
        }
    

    Note:

    1) If multiple lines labels, don't forget set the numberOfLines to 0.

    2) Don't forget label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)

    0 讨论(0)
  • 2020-12-24 13:20

    You can use the tableview's delegate instead of the tableView itself.

    id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    

    Check the answer here

    UPDATED

    In new iOS versions and using correct auto constraints you don't need to reference the cell anymore to calculate the cell's height.

    Check here

    https://www.raywenderlich.com/129059/self-sizing-table-view-cells

    0 讨论(0)
  • 2020-12-24 13:20

    If you want to access the cells use tags. But as @Jhaliya has said it is better to calculate the height based on the content of the cell. If your regard is with the position of the cell then I would suggest you to use scroll view and implement the cells yourself wherever you want.

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