2013-06-26 2 views
2

UITableViewCell에는 셀에 레이블이 있고 UITextField이 미리 채워져 있습니다. 레이블이 "map"인 셀이 있고 그 셀에 단추를 추가하려면 어떻게해야합니까?
시도했지만이 작동하지 않습니다.
여기에 코드입니다 :UITableViewCell에 단추를 추가하는 방법

UITAbleViewCell에 버튼을 추가 할 수있는 몇 가지 방법이있다
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"rateSheetCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    if (indexPath.row == 0) 
    { 
     ((UILabel*)[cell viewWithTag:100]).text = @"Title"; 
     ((UITextField*)[cell viewWithTag:101]).placeholder = @"Title"; 
     ((UITextField*)[cell viewWithTag:101]).text = arrayResourceColumns[indexPath.section][indexPath.row][@"resource_title"]; 
     ((UITextField*)[cell viewWithTag:101]).keyboardType = UIKeyboardTypeDefault; 
    } 
    else 
    { 
     if([arrayResourceColumns[indexPath.section][indexPath.row][@"label"] isEqualToString:@"OT ($)"]){ 

      NSInteger desiredLabel = (NSInteger)(arrayResourceColumns[indexPath.section][indexPath.row][@"label"]); 

      //NSString *desiredLabel = arrayResourceColumns[indexPath.section][indexPath.row][@"label"]; 
      NSLog(@"Label is>>>>--------%ld", (long)desiredLabel); 

      if (indexPath.row == desiredLabel) { 
       UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [b setFrame:CGRectMake(0, 0, 50, 50)]; 
      } 


     } 
     ((UILabel*)[cell viewWithTag:100]).text = arrayResourceColumns[indexPath.section][indexPath.row][@"label"]; 
     ((UITextField*)[cell viewWithTag:101]).placeholder = ((UILabel*)[cell viewWithTag:100]).text; 
     ((UITextField*)[cell viewWithTag:101]).text = arrayResourceColumns[indexPath.section][indexPath.row][@"attribute_value"]; 

     if (indexPath.row == [arrayResourceColumns[indexPath.section] count] - 1) 
     { 
      ((UITextField*)[cell viewWithTag:101]).enabled = NO; 
     } 
    } 
} 
+0

당신이 셀의 파단에 액세스 할 수 태그 (100) 및 (101)을 사용하는 것과 UITableViewCell을 사용자 정의하는 방법을 튜토리얼. 그러나 당신은 그들을 세포에 어디에 추가 했습니까? 그것들은'cell == nil'의 경우 btw에 추가되어야합니다. –

+0

@Khanh 태그는 textlabel 및 textfield이고 textlabel이고 textfield는 스토리 보드의 uitableviewcell 프로토 타입에 추가되었습니다 – Ammad

+0

@ user1699419 자신 만의 사용자 정의 테이블보기 셀을 사용할 수 있습니다. 이 내부 UIButton, UILabel 및 UITextField 등 하위 뷰를 추가 할 수 있습니다 ... Hv 솔루션있어? –

답변

2

:
1) 스토리 보드를 사용하는 경우이 버튼 전지 프로토 타입을 만들 수 있습니다 당신이 tag으로이 버튼에 액세스 할 수 있습니다 이상이 속성
2) 및 서브 클래스 UITableViewCell
추가 버튼을) 추가 버튼 tableView:cellForRowAtIndexPath: 방법에서는 셀

UPD
Here is 스토리 보드

+0

(미안이 잘못되었습니다) 감사합니다 @ KhanhNguyen –

+2

@HermannKlecker'UITableViewCell'는'UIView', * UIViewController가 아니며'viewDidLoad'와'init '서브 뷰를 추가 할 수있는 적절한 장소입니다. –

+0

옵션 3은 초보자에게 권장할만한 것은 아니지만 매우 쉽습니다. 물론 그렇지 않습니다. 물론 버튼이 모든 셀에있는 경우가 아니라면 cellForRow ...는 그것을위한 좋은 장소. f 버튼이 모든 셀에 없으면 셀을 볼 때마다 셀에 버튼을 추가하거나 스크롤 할 때 재사용 셀에 나타나는 버튼과 같은 이상한 효과를 피하기 위해 재사용 로직과 함께 스마트 코딩이 필요합니다. 기타 –

관련 문제