UIButtonをテーブルセルに動的に追加する必要があります - 以下の要件を参照してください。ユーザがセルを選択した場合UITableViewCellにUIButtonを動的に追加するにはどうすればいいですか?
、私は単に
-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath {
selIndPath = [indexPath retain];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
にそのテーブルのセルの高さを調整してい上記の方法は、その選択された行の高さを調整するためのtableViewを強制します。しかし、上記の方法でUIButtonを追加すると、ボタンは表示されません。
私は[self.tableView reloadData]に興味がありません。
大変助かりました。
** * ** EDITED* ** * ***
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (selIndPath && selIndPath == indexPath) {
//UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
// UIButton* btn = (UIButton*)[[cell contentView] viewWithTag:1234];
// btn.frame = CGRectMake(40, 60, 60, 24);
// [cell setNeedsDisplay];
return 90;
}
return 64;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
}
// THIS LINES OF CODE WORKS ONLY ON reloadData
if (selIndPath && selIndPath == indexPath) {
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(40, 60, 60, 24);
btn.tag = 1234;
[btn setTitle:@"Hi" forState:UIControlStateNormal];
[cell.contentView addSubview:btn];
}
// Configure the cell.
cell.textLabel.text = @"Loading..";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selIndPath = [indexPath retain];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
//[self.tableView reloadData];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
を追加するには、あなたがUIButtonを追加するために使用していたコードを表示することができますか? – Louis
返信いただきありがとうございます、私は私の質問にいくつかの追加のコードを追加しました。それはあなたがそれを理解するのに役立つかもしれません。 – prathumca