2014-06-18 3 views
0

나는 tableview와 같은 양식을 만들려고 노력하고 있습니다. 다른 애플 리케이션에서 이걸 1000 번이나 보았 기 때문에 간단 할 것 같습니다.UITableField 내부에서 UITableViewCell 내부의 텍스트를 편집하려면 어떻게해야합니까?

내 시도는 다음과 같습니다. UITableViewCell의 내용보기에 UITextField를 추가했습니다. 또한 UITextFieldDelegate 메서드를 구현하고 선택 스타일을 none으로 설정해 보았습니다.

내가 뭘 잘못하고 있는지 알 수 있습니까?

저는 대리자 및 데이터 소스 메서드와 사용자 지정 UITableViewCell 하위 클래스를 처리하기 위해 별도의 NSObject로 통조림을 사용하는 UITableViewController를 사용하고 있습니다.

지금 내가 셀을 탭하면 firstFirstResponder가되지 않으며 전혀 발생하지 않습니다. setSelected에서 일부 중단 점을 설정하려고 시도했지만 트리거되지 않은 것으로 보입니다.

내 tableViewCell 코드

@implementation HSTableViewCell 

- (void)willMoveToSuperview:(UIView *)newSuperview { 

    [self.contentView addSubview:self.textField]; 

    NSDictionary *elements = NSDictionaryOfVariableBindings(_textField); 

    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_textField]|" 
                      options:NSLayoutFormatDirectionLeftToRight 
                      metrics:nil 
                      views:elements]; 

    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_textField]-|" 
                      options:NSLayoutFormatDirectionLeftToRight 
                      metrics:nil 
                       views:elements]; 

    [self.contentView addConstraints:verticalConstraints]; 
    [self.contentView addConstraints:horizontalConstraints]; 

} 

- (void)prepareForReuse { 
    [self.textField removeFromSuperview]; 
    [self.contentView removeConstraints:self.contentView.constraints]; 
    self.textField = nil; 
} 


- (UITextField *)textField { 
    if (!_textField) { 
     _textField = [UITextField new]; 
     [_textField setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     [_textField setPlaceholder:@"Test"]; 
    } 
    return _textField; 
} 


@end 

있는 UITableViewController 초기화

- (UITableViewController *)tableViewController { 
    if (!_tableViewController) { 
     _tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain]; 
     [_tableViewController.tableView setDataSource:self.tableViewDataManager]; 
     [_tableViewController.tableView setDelegate:self.tableViewDataManager]; 
     [_tableViewController.tableView registerClass:[HSTableViewCell class] forCellReuseIdentifier:identifer]; 
    } 
    return _tableViewController; 
} 

와 UITableViewDelegate/데이터 소스

@implementation HSTableViewDataManger 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 5; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath]; 
    return cell; 
} 

@end 

사람이 관심이 있다면, 테스트 프로젝트는

이하

이것은 아주 베어 본입니다. 원하는 동작을 달성하기 위해 필요한 것을 정확히 분리하려고합니다.

https://dl.dropboxusercontent.com/u/2396540/TextFieldInCellTest.zip

+0

실제로 의미가없는 많은 것이 실제로 있습니다. 나는 당신을 도우려고 노력할 것입니다! 스토리 보드를 사용하여이 양식을 작성하거나 의도적으로 프로그래밍 방식으로 어떻게 작동하는지 알고 싶다면 솔직히 말해서 더 쉽습니다. – nburk

+0

무엇이 문제입니까? 이 코드는 어떤 결과를 제공합니까? – rdelmar

+0

저는 이것이 프로그래밍 방식으로 어떻게 작동하는지 알고 싶습니다. 그러나 이것은 현재 내가 도와주고있는 훨씬 더 큰 앱의 조각이며,이 시점에서 스토리 보드로 변환 할 수 없습니다. – Weston

답변

1

나는 willMoveToSuperview에서 텍스트 뷰를 추가하는 작동하지 않는 이유를 모르겠어요,하지만 그렇게 할 수있는 특별한 장소입니다. 거기에있는 코드를 initWithStyle : reuseIdentifier :에 넣고 prepareForReuse 메소드를 삭제해야합니다 (텍스트 필드는 init 메소드에서 한 번만 추가되므로 삭제할 필요가 없습니다). 편집 가능한 텍스트 필드가 있어야합니다.

+0

감사합니다. 나는 ALL MORNING 주변에서 묻고있다. 아무도 나를 도울 수 없었습니다. 내가 잃어버린 뭔가 어리 석다는 것을 알았습니다. – Weston

+0

@Kronusdark, 데이터 관리자 클래스에서 UITableViewCell * cell = ...을 HSTableViewCell * cell =로 변경해야합니다. 그러면 사용자 정의 셀의 속성에 액세스 할 수 있습니다 (실제로 HSTableViewCell을 반환합니다. 하지만 텍스트 필드에 액세스하려고하면 오류가 발생합니다.) – rdelmar

+0

예, 알고 있습니다. 이 문제를 파악하기위한 테스트 스파이크 일 뿐이며 실제 코드는 훨씬 좋습니다. – Weston

관련 문제