簡體   English   中英

UITableViewCell 按鈕以展開單元格中的文本 - 如何執行此操作?

[英]UITableViewCell button to expand text in a cell - how to do this?

我有一個包含 10 個部分的列表的UITableView ,每個部分包含一個textLabel

我將每個單元格中的文本數量限制為 10 行,並放置了一個按鈕以允許用戶擴展單元格中的文本。

問題是,當在“顯示更多”(如果單元格未展開)和“顯示更少”(如果單元格已展開)之間單擊時,我想更改按鈕上的文本。 我怎樣才能做到這一點?

現在我在cellForRowAtIndexPath有這個代碼。 它似乎無法正常工作,有時當只顯示 10 行時,按鈕會顯示“顯示較少”。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ButtonCell"];

if (!cell) {

  UIButton *readmoreButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [readmoreButton setTitle:@"Read More" forState:UIControlStateNormal];
  [cell addSubview:readmoreButton];

}
else {

  NSEnumerator *e = [cell.subviews objectEnumerator];
  id object;
  while (object = [e nextObject]) {
    if ([object class]==UIButton.class && [((UIButton*)object).titleLabel.text isEqualToString:@"Read Less"])
      ((UIButton*)object).titleLabel.text = @"Read More";
    }
}

當用戶單擊按鈕時調用以下方法。 NSMutableArray ProposalInfo跟蹤哪個單元格被/未擴展。

- (void)toggleFullProposal:(id) sender {

// get cell
NSIndexPath *index = [self.tableView indexPathForCell:(UITableViewCell *)[sender superview]];

ProposalInfo *proposalInfo = [self.proposalInfoArray objectAtIndex:index.section];

if ([proposalInfo expanded]==YES) {

    [proposalInfo setExpanded:NO];

    [(UIButton *)sender setTitle:@"Read More" forState:UIControlStateNormal];

}  else {

    [proposalInfo setExpanded:YES];

    [(UIButton *)sender setTitle:@"Read Less" forState:UIControlStateNormal];

}

// create index paths to reload
NSMutableArray *indexPathsToReload = [[NSMutableArray alloc] init];
[indexPathsToReload addObject:[NSIndexPath indexPathForRow:0 inSection:index.section]];

// refresh
[self.tableView reloadRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationNone];

}

單擊按鈕時,請確保現在將全文設置為單元格。 然后只重新加載單元格並注意您需要在layoutSubviews:修復標簽的高度,然后為heightForRowAtIndexPath:返回適當的高度。

((MyCell*)[tableview cellForRowAtIndexPath:buttonClickedindexPath]).fullText = myModel.fullText;

並在layoutSubviews :自定義類單元格的方法中展開標簽的框架以適合其中的文本,然后在heightForRowAtIndexPath:返回適當的高度,這應該可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM