2011-12-26 4 views
0

테이블 셀에 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]; 
} 
+0

은 당신이있는 UIButton을 추가 사용하던 코드를 표시 할 수 있습니다 추가? – Louis

+0

답장을 보내 주셔서 감사합니다. 제 질문에 몇 가지 추가 코드를 추가했습니다. 그걸 이해하는 데 도움이 될 수 있습니다. – prathumca

답변

2

당신이 그것을 추가하면 버튼의 추가가 작동하지 않습니다 셀에 cellForRowAtIndexPath :. 당신이해야 할 것은 UITableViewCell이되는 사용자 정의 셀에서

(대제 Changing the position of custom UIButton in custom UITableViewCell에이 질문의 대답을 참조하십시오) 사용자 정의를 만드는 것입니다, 당신은 버튼을 무엇을 추가하는 방법, 방법 물론

-(void) addButtonToCell 
{ 
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn.frame = CGRectMake(40, 60, 60, 24); 
    btn.tag = 1234; 
    [btn setTitle:@"Hi" forState:UIControlStateNormal]; 
    [self.contentView addSubview:btn]; 

} 

를 만들 당신이이 방법에 전달하는 주장은 당신에게 달려 있지만, 당신은 요지를 얻습니다. cellForRowAtIndexPath에서

: 단순히

if (selIndPath && selIndPath == indexPath) { 
[cell addButtonToCell]; 
} 
+0

답장을 보내 주셔서 감사합니다. [tableView reloadData]를 호출하는 경우에만 작동합니다. 앞서 말했듯이, 나는 reloadData에 관심이 없다. 내 테이블은 매우 큰 데이터를 가지므로 행 높이 변경의 애니메이션 영향을 무효화합니다. – prathumca

+0

@prathumca :이 방법을 사용하는 방법 - (void) reloadRowsAtIndexPaths : (NSArray *) indexPaths withRowAnimation : (UITableViewRowAnimation) 애니메이션 테이블보기에서. 그러면 원하는 행만 다시로드됩니다. – MadhavanRP

+0

나는 그것을 시도하고 있었지만 지금은 운이 없다. 애니메이션이 부드럽지 않고 버튼이 제대로 표시/정렬되지 않습니다. 어떤 도움을 정말 감사드립니다. – prathumca

관련 문제