2016-12-14 44 views
1

我有一个非常奇怪的问题,那就是当我第一次调用didSelectRowAtIndexPath方法时,它不起作用。尝试调用`didSelectRowAtIndexPath`但不是第一次调用

如果我再次调用它,它会完成。当我第一次设置断点时,didSelectRowAtIndexPath中的代码也会执行,但不会推送到下一个控制器。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    tableView.allowsSelection = NO; 
    ChatRoom *room = [_rooms objectAtIndex:indexPath.row]; 
    if (!room.conv) { 
     room.conv = [[_im imClient] conversationForId:room.convid]; 
    } 
    CDChatRoomViewController* chatRoomVC=[[CDChatRoomViewController alloc] init]; 
    chatRoomVC.room = room; 
    chatRoomVC.convId = room.convid; 
    chatRoomVC.converSation = room.conv; 
    chatRoomVC.hidesBottomBarWhenPushed = YES; 
    [self.navigationController pushViewController:chatRoomVC animated:YES]; 
} 
+0

检查方法名称正确 – Furqan

+0

这个问题只发生在iPhone上,当我使用模拟器来测试它,一切都OK。 – Adam

+0

你可以请张贴一些代码! –

回答

0

这是提醒一次检查方法,有两种方法看起来很彼此相同

//当你的电池是越来越选择此方法,下面将调用

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
//Cell selected 
} 

//当你的单元格被取消选择时,This Below方法会被调用。当您在tableview中选择一个单元格,这可能是你的情况

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
//Cell deselected 

} 

,选定单元格的tableview火取消方法(除了多重选择打开),

希望这将证明回答您的问题。

+0

谢谢,我确定我所说的方法是正确的。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { – Adam

+0

您能分享代码片段 –

+0

我们可以帮助您更好地 –

0

首先您需要了解如何使用segue在视图控制器之间传输数据。这是在View Controller之间传输数据的更快,更好的方式。还有其他的方式,比如Delegate。

在当前视图控制器,请声明此

var selectedCD : Int = 0 

在你didSelectRowAtIndexPath方法,请修复这些。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    yourTableName.deselectRowAtIndexPath(indexPath, animated: true) 
    // There is no need to do "tableView.allowsSelection = NO;" 
    selectedCD = indexPath.row 
    self.performSegueWithIdentifier("goToChatRoom", sender: self) 
    // "goToChatRoom" is the name of segue which will go to CDChatRoomViewController which you need to set this at Storyboard. 

} 

截至CDChatRoomViewController

self.hidesBottomBarWhenPushed = true 

ViewWillAppear()然后创建在您做的UITableView委托方法如下SEGUE方法,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ 
    if segue.identifier == "goToChatRoom"{ 
     let destinationController = segue.destinationViewController as! CDChatRoomViewController 
     // handle selectedCD logic here 
     ChatRoom *room = [_rooms objectAtIndex:indexPath.row]; 
     if (!room.conv) { 
      room.conv = [[_im imClient] conversationForId:room.convid]; 
     } 
     destinationController.room = room; 
     destinationController.convId = room.convid; 
     destinationController.converSation = room.conv; 
    } 
} 

请编辑你的一些Objective-C代码在我回答因为我真的不知道你在做什么。

您可以在这里学到的使用塞格斯:

Sending data with Segue with Swift

尝试这种方式

记住,不要加的实现[即需要做哪些使用CPU或内存的处理]上didSelectRowAtIndexPath

相关问题