表视图委托方法并非全部被调用

时间:2013-03-20 12:22:38

标签: ios objective-c tableview

在我的代码中,我正在使用一个tableview,其中一些方法被调用,而其他方法则没有。

在initwithframe中

_table.delegate = self;
_table.dataSource = self;

这叫做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TRAutocompleteCell";

    id cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
        cell = [_cellFactory createReusableCellWithIdentifier:identifier];

    NSLog(@"got here 3");

    NSAssert([cell isKindOfClass:[UITableViewCell class]], @"Cell must inherit from UITableViewCell");
    NSAssert([cell conformsToProtocol:@protocol(TRAutocompletionCell)], @"Cell must conform TRAutocompletionCell");
    UITableViewCell <TRAutocompletionCell> *completionCell = (UITableViewCell <TRAutocompletionCell> *) cell;

    id suggestion = self.suggestions[(NSUInteger) indexPath.row];
    NSAssert([suggestion conformsToProtocol:@protocol(TRSuggestionItem)], @"Suggestion item must conform TRSuggestionItem");
    id <TRSuggestionItem> suggestionItem = (id <TRSuggestionItem>) suggestion;

    [completionCell updateWith:suggestionItem];

    return cell;
}

但是这不是在同一个文件中调用的。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"got here 5");
    id suggestion = self.suggestions[(NSUInteger) indexPath.row];
    NSLog(@"got here 4");
    NSAssert([suggestion conformsToProtocol:@protocol(TRSuggestionItem)], @"Suggestion item must conform TRSuggestionItem");

    self.selectedSuggestion = (id <TRSuggestionItem>) suggestion;

    _queryTextField.text = self.selectedSuggestion.completionText;
    [_queryTextField resignFirstResponder];

    if (self.didAutocompleteWith)
        self.didAutocompleteWith(self.selectedSuggestion);

}

2 个答案:

答案 0 :(得分:0)

问题不在于实施,而在于我的手势识别。

我在我的视图中添加了一个列表,以便在我的文本框之外捕捉到被点击的事件。我不知道为什么这两个会发生冲突,但当我删除它时,它正在发挥作用。

tableview用于在我的文本框中自动完成。

答案 1 :(得分:-1)

你做过:

yourTableView.delegate = self;
yourTableView.dataSource = self;

创建tableView时。

如果是.xib文件,你是否连接了委托和dataSource?