第一次点击时没有调用UITableView didSelectRowAtIndexPath

meg*_*eng 27 uitableview didselectrowatindexpath ios5

我遇到了UITableView的ios 5的didSelectRowAtIndexPath问题,这在ios 4中是正确的.

我第一次点击表中的任何一行时,该方法不会被调用.一旦我选择了另一行,它就会调用didSelectRowAtIndexPath,但会传递最后一个indexPath.

我已经设置了tableView.delegate,它可以在ios4中正确运行.

MainView.xib
UITabBarController
     --UINavigationController
         --**UITableViewController**
Run Code Online (Sandbox Code Playgroud)

有什么建议?

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - Table view data source

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

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

- (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.textLabel.text = [[NSString alloc]initWithFormat:@"%d",indexPath.row];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d",indexPath.row);
}
Run Code Online (Sandbox Code Playgroud)

Jan*_*ray 34

您可能已实施(请注意取消选择):didDeselectRowAtIndexPath

...因此在选择取消选择第一个的新单元时触发,并将indexPath记录为第一个.

解: didSelectRowAtIndexPath

是的,他们看起来非常相似,并没有帮助这didDeselectRowAtIndexPath是Xcode的自动完成选择大多数时间的第一种方法.

完整代码:

// right
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {}
Run Code Online (Sandbox Code Playgroud)


Jah*_*ari 0

请将 Xib 的委托连接到文件所有者。然后尝试一下就会有效。