테이블 셀에 UIButton을 동적으로 추가하고 싶습니다. 아래 요구 사항을 참조하십시오. UITableViewCell에 UIButton을 동적으로 추가하는 방법은 무엇입니까?
사용자가 셀을 선택
, 단순히 그 선택된 행의 높이를 조정-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath {
selIndPath = [indexPath retain];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
상기 방법에있는 tableView 그런 테이블 셀의 높이를 강제로 조정하고있다. 그러나 위의 메서드에서 UIButton을 추가하면 단추가 표시되지 않습니다.
[self.tableView reloadData]에 관심이 없습니다.
도움을 주시면 감사하겠습니다.
** * ** 편집을 할* ** * ***
- (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