UITableView
を含む 10 個のセクションのリストを持っています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];
}