increase uitableviewcell height simultaneously increasing the inner UITextView

前端 未结 8 1722
花落未央
花落未央 2021-02-02 17:13

I create a UITableView with different types of UITableViewCell depending on the type of content to display. One of this is a UITableViewCell

8条回答
  •  忘掉有多难
    2021-02-02 17:31

    Siba Prasad Hota's code probably will do the trick (You need reference to table view from cell level), but I have another, longer approach. I always do such stuff in this way, because I like to have all things separated (MVC pattern). If I were You, I would do this like that (code from head):

    Cell parent protocol:

    @protocol CellParent 
    @required
    @property (nonatomic, strong) UITableView *tableView;
    @end
    

    Cell model:

    @interface CellModel
    @property (nonatomic, assign) BOOL hasTextView;
    @property (nonatomic, strong) NSString *textViewContent;
    -(float)getCurrentHeightForCell;//implement calculating current height of cell. probably     2 * SOME_MARGIN + height of temporary textView with textViewContent variable
    

    Cell

    @interface MyCell
    @property (nonatomic, strong) CellModel *dataModel;
    @property (nonatomic, weak) id parent;
    @property (nonatomic, strong) UITextView *textView;
    - (id)initWithStyle:(UITableViewCellStyle)style andModel:(CellModel*) model;
    

    with implementations like this:

    (id)initWithStyle:(UITableViewCellStyle)style andModel:(CellModel*) model
    {
        self = [super initWithStyle:style reuseIdentifier:@"MyCell"];
        if (self) 
        {
            [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
            self.dataModel = model;
        }
        return self;
    }
    
    
    -(void) setDataModel:(CellModel *)dataModel
    {
        _dataModel = dataModel;
        if(_dataModel.hasTextView)
        {
            //show text view
        }
        else
        {
            //hide text view
        }
        //do other cell modifications
    }
    
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
    {
        self.dataModel.textViewContent = textView.text;
        [self.parent.tableView beginUpdates];
        [self.parent.tableView endUpdates];
        return YES;
    }
    

    Controller with table view

    -(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {  
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
        if (cell == nil) 
        {
            cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault andModel:          [self.cellsModels objectAtIndex:indexPath.row]];
        }
        cell.dataModel = [self.cellsModels objectAtIndex:indexPath.row];
        cell.parent = self;
        return cell;
    }
    
    -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath     *)indexPath
    {
        return [((CellModel*)[self.tableContentArray objectAtIndex:indexPath.row]) getCurrentHeightForCell];
    }
    

提交回复
热议问题