2010-05-24 9 views
1

프로그래밍 방식으로 내 tableview 셀 중 하나에 UITextFiled를 추가하려고합니다. 어떻게하면 좋을까요?iPhone : TableView 셀 안의 UITextField?

다음과 같은 가정하에 사용하는 코드는 무엇입니까?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; 
     cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     // Don't let the user click when not in editing mode 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text = @"Title"; 
      // This is where I want to add a text field 
      cell.detailTextLabel.text = @"test"; 
      cell.editing = YES; 
     break; 
    } 

    return cell; 
} 

답변

1

이 시도 :

[cell.contentView addSubview:[[[UITextField alloc] initWithFrame:CGRectMake(...)] autorelease]]; 
+0

좋아, 그 작동합니다. 하지만 이제는 텍스트 필드에 대한 포인터를 어떻게 갖게 될까요? 그래서 기본 텍스트와 콘센트를주는 것과 같은 일을 할 수 있습니까? –

+0

해당 텍스트 필드에 대한 포인터를 가질 수 있는지 확인하십시오. 예를 들어, 포인터 클래스 멤버를 생성하고, 생성 된 인스턴스를 셀에 추가하는 것보다 생성자에 UITextField 인스턴스를 생성합니다. 예 : [cell.contentView addSubview : instance]; – Yakov

+0

좋아, 내가 말한대로,하지만 텍스트 필드가 표시되지 않습니다, 나는 UITextField 인스턴스를 만들고 그것을 내 포인터 - 클래스 멤버에 할당하려고 시도 rideTitle = [[UITextField alloc] initWithFrame : CGRectMake (12, 45, 260, 25]]; 그러나 그것은 작동하지 않는 것 같습니다. 아이디어? –