Is there any way refresh cell's height without reload/reloadRow?

前端 未结 4 1122
忘掉有多难
忘掉有多难 2021-01-31 09:41

I make a view like imessage, just input text into the bottom text view. I use table view to do this, and the text view in the last cell. when I input long text that more than on

4条回答
  •  Happy的楠姐
    2021-01-31 10:09

    I've written a subclass of UITableViewCell to handle this functionality.

    .h file:

    #import 
    
    @protocol AECELLSizeableDelegate;
    
    @interface AECELLSizeable : UITableViewCell
    
    @property (weak, nonatomic) id  delegate;
    
    @property IBOutlet UIView *viewMinimized;
    @property IBOutlet UIView *viewMaximized;
    @property BOOL maximized;
    
    @property CGFloat height;
    
    - (IBAction)clickedConfirm:(id)sender;
    - (IBAction)clickedCancel:(id)sender;
    
    - (void)minimizeForTableview: (UITableView*)tableView;
    - (void)maximizeForTableview: (UITableView*)tableView;
    - (void)toggleForTableview: (UITableView*)tableView;
    
    @end
    
    @protocol AECELLSizeableDelegate 
    
    - (void)sizeableCellConfirmedForCell: (AECELLSizeable*)cell;
    - (void)sizeableCellCancelledForCell: (AECELLSizeable*)cell;
    
    @end
    

    .m file:

    #import "AECELLSizeable.h"
    
    @implementation AECELLSizeable
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    - (void)minimizeForTableview: (UITableView*)tableView
    {
        self.maximized = NO;
        [self.viewMinimized setHidden:NO];
        [self.viewMaximized setHidden:YES];
        self.height = self.viewMinimized.frame.size.height;
        [tableView beginUpdates];
        [tableView endUpdates];
    }
    
    - (void)maximizeForTableview: (UITableView*)tableView
    {
        self.maximized = YES;
        [self.viewMinimized setHidden:YES];
        [self.viewMaximized setHidden:NO];
        self.height = self.viewMaximized.frame.size.height;
        [tableView beginUpdates];
        [tableView endUpdates];
    }
    
    - (void)toggleForTableview:(UITableView *)tableView
    {
        if (self.maximized) {
            [self minimizeForTableview:tableView];
        } else {
            [self maximizeForTableview:tableView];
        }
    }
    
    - (void)clickedConfirm:(id)sender
    {
        [self.delegate sizeableCellConfirmedForCell:self];
    }
    
    - (void)clickedCancel:(id)sender
    {
        [self.delegate sizeableCellCancelledForCell:self];
    }
    
    @end
    

    Example Usage:

    1. Create a UITableViewController with a static UITableView in IB
    2. Add a cell to the tableview that is a AECELLSizeable (or subclass of it)
    3. Create two UIViews in this cell. One UIView will be used for the content visible while minimized, the other will be used for the content visible while maximized. Ensure these cells start at 0 on the y axis and that their height is equal to that of the height you wish to have the cell for each state.
    4. Add any subviews to these two views you desired. Optionally add confirm and cancel UIButtons to the maximized UIView and hook up the provided IBActions to receive delegate callbacks on these events.
    5. Set your tableview controller to conform to the AECELLSizeableDelegate and set the cell's delegate property to the tableview controller.
    6. Create an IBOutlet in your UIViewController's interface file for the AECELLSizeable cell.
    7. Back in IB, ensure the cell's initial height is that of the minimized version and connect the IBOutlet you just previously created.
    8. Define the tableview's heightForRowAtIndexPath callback method in the tableview controller's implementation file as such:

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if (indexPath.row == 0) {
                //Sizeable Cell
                return self.cellSizeable.height;
            } else {
                return [super tableView:tableView heightForRowAtIndexPath:indexPath];
            }
        }
      
    9. In your tableview controller's viewDidLoad method, call minimizeForTableview on your cell to ensure it starts off minimized.

    10. Then, call maximizeForTableview: on the cell when it is selected via the didSelectRowAtIndexPath callback from the tableview (or however else you would like to handle it) and call the minimizeForTableview: method on the cell when you receive the canceled / confirmed delegate callbacks from the cell (or, again, however else you'd like to handle it).

提交回复
热议问题