我有一个非常奇怪的问题,那就是当我第一次调用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];
}发布于 2016-12-14 14:04:40
建议检查一次您的方法,有两个方法看起来彼此非常相同
//当您的单元格被选中时,将调用下面的方法
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//Cell selected
}//当您的单元格被取消选择时,将调用下面的方法。这可能是你的案子
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
//Cell deselected
}在tableview中选择单元格时,tableview会触发对所选单元格的取消选择方法(多选开启时除外)
希望这将证明你的问题的答案是正确的。
发布于 2016-12-14 16:18:04
首先,您需要学习如何使用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大学的ViewWillAppear()
self.hidesBottomBarWhenPushed = true然后在执行UITableView委托方法的位置创建以下段方法,
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代码,因为我真的不知道你在做什么。
你可以在这里学习使用segues:
Sending data with Segue with Swift
试着那样做
请记住,不要添加需要在didSelectRowAtIndexPath.上执行使用
或的处理的实现
https://stackoverflow.com/questions/41135041
复制相似问题