2017-03-20 4 views
0

1.セル内のコンテンツが長すぎる場合は、セルの展開、または縮小した後、UITableViewがセルの位置の後ろにスクロールします。iOSでlong longコンテンツを使用したUITableViewCellの拡張

2.セルが展開を開始した場所にロールバックします。

私のコード:

((PartnershipsTableViewCell *)cell).commentSpreadButtonClickHandler = ^() { 
     // just call - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
     [weakSelf.tableView beginUpdates]; 
     [weakCell configUI]; 
     [weakSelf.tableView endUpdates]; 

     // if here use "[weakSelf.tableView reloadData]", 
     // it can be correct, 
     // Unless on the first cell which have the expansion button. 
    }; 

、その後のUITableViewのセルの高さを更新します。しかし、結果は私が

- (void)configUI { 
    if ([self.baseModel isKindOfClass: [UserWorldDynamicModel class]]) { 
     self.model = (id)self.baseModel; 
    } 
    [self setupValue]; 
} 

- (void)setupValue { 
    // setup the property value, and update the constraints with masonry 
} 


// the button : read less or read more 
- (void)setSpreadButton { 
    NSString *text = self.model.isContentOpen ? @"read less" : @"read more"; 
    if (!self.spreadButton) { 
     self.spreadButton = [MYSUtil createButtonWithTitle: text target: self sel: @selector(spreadButtonClick:) image: nil font: Font14 color: DarkBlueTextColor cornerRadius: 0]; 
     self.spreadButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
    } 
    if (self.model.shouldShowSpreadButton) { 
     if (!self.spreadButton.superview) { 
      [self.whiteBackgroudView addSubview: self.spreadButton]; 
     } 
     [self.spreadButton setTitle: text forState: UIControlStateNormal]; 
     self.spreadButton.selected = [self.model.isSpreadState intValue]; 
     self.spreadButton.frame = CGRectMake(CGRectGetMinX(self.contentLabel.frame), CGRectGetMaxY(self.contentLabel.frame), 80, 30); 
     self.tempView = self.spreadButton; 
    } else { 
     if (self.spreadButton.superview) { 
      [self.spreadButton removeFromSuperview]; 
     } 
    } 
} 

    // calculate the height of the label and compare it with the fixed value 
    NSMutableAttributedString *string = [self createMutableAttibuteStringWithNSString: text withFont: font]; 
    self.contentLabel.attributedText = string; 

    // here calculate maxContentLabelHeight with 
    CGFloat maxContentLabelHeight = self.contentLabel.font.pointSize * (numberOfLines + 1) + 16; 

    // here calculate the NSMutableAttributedString's height 
    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(width, MAXFLOAT)]; 
    YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text: string]; 
    CGSize size = textLayout.textBoundingSize; 
    CGFloat height = size.height; 

    // then compare the NSMutableAttributedString's height with the fixed value. if true, show the spreadButton 
    if (height > maxContentLabelHeight) { 
     self.model.shouldShowSpreadButton = YES; 
     // storage the real height and temp height, use to calculate the tableView's contentOffset, when cell from expansion state to shrinking state in block. 
     self.model.contentHeight = height; 
     self.model.tempContentHeight = maxContentLabelHeight; 
    } 

    // if height > maxContentLabelHeight and the property "isContentOpen" of the viewModel, the height value is maxContentLabelHeight, Or not, the height value is height 
    if (!self.model.isContentOpen && height > maxContentLabelHeight) { 
     height = maxContentLabelHeight; 
    } 

    // no matter cell is expansion state or shrinking state, reset label's frame. 
    self.contentLabel.frame = CGRectMake(x, CGRectGetMaxY(self.headerImageView.frame) + Margin_Top, width, height); 
+0

答えを編集して 'configUI'メソッドを含めることはできますか? – pckill

+0

はい、できますが、コードは非常に長くなります。メソッドを使用してuitableviewcellのコンテンツをリセットし、セルの高さを計算する – thomas

+1

次に、関連のない部分を編集します。通常は、問題を特定することをお勧めします。問題を解決するためのヒントを提供することができます。 – pckill

答えて

0

readMore/readLess、mainQueue上のtableView reloadData前

をブロックすることがスクロールする必要があるのtableViewの位置を計算するために使用し、contentOffsetのレコード望むものではありません。このように:

CGPoint point = weakSelf.tableView.contentOffset; 

reloadData:refresh tableView on mainQueue。 reloadDataが完了したときに、tableViewを展開した位置にスクロールします。拡張状態から状態を縮小するのtableView、および拡張状態の高さは、画面の高さの70%より大きくなると、のtableViewスクロール(70%がミリアンペアの条件である、あなたが変更することができ、あなたの条件に応じてある)

- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 
    // here is your code 
    PartnershipsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifierString]; 
    [cell config]; 
    ((PartnershipsTableViewCell *)cell).spreadButtonClickHandler = ^() { 
     CGPoint point = weakSelf.tb.contentOffset; 
     [weakSelf.tb reloadData]; 
     if (!baseModel.isContentOpen && baseModel.contentHeight > SCREEN_HEIGHT * 0.7) { 
      point.y -= baseModel.contentHeight - baseModel.tempContentHeight; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       weakSelf.tb.contentOffset = point; 
      }); 
     } 
    }; 
    return cell; 
} 


- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath { 
    return 70; 
} 

私の問題では、あなたのコードでfollowメソッドを使用すると、別の興味深い問題が見つかりました。あなたの細胞に大きな変化、特に細胞の高さがある場合。メソッド[tableView reloadData]を使用する場合は、followメソッドを使用しない方がよいでしょう。

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
     return 70; 
    } 

その後、UIを更新するために、[のtableView reloadData]メソッドを使用し、のtableViewは、任意の位置にスクロールしますので、私はそれを削除します。このメソッドは、セルの推定高さに使用するので、推定値と実際の値の差が大きい場合は、tableViewのcontentSizeを推定するために使用します。メソッド[tableView reloadData]を使用すると、tableViewをどこにでもスクロールさせます。私は知る方法、これは私のための痛いプロセスです)。

私は問題を解決し、自分自身にいくつかのメモを残し、すべての人に対して、私の解決策もあなたを助けることを願っています。

@pckillと@ Theheistのおかげで、あなたの提案なしで、私は完璧な、私はあなたの質問を解決することはできません非常にありがとう。

@テオリスト私は自分のコードを書き直しました。恐らく、読みやすさは今より良いでしょう。

関連する問題