首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试调用`didSelectRowAtIndexPath`但不是第一次调用

尝试调用`didSelectRowAtIndexPath`但不是第一次调用
EN

Stack Overflow用户
提问于 2016-12-14 12:47:35
回答 2查看 560关注 0票数 1

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

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

代码语言:javascript
运行
复制
- (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];
}
EN

回答 2

Stack Overflow用户

发布于 2016-12-14 14:04:40

建议检查一次您的方法,有两个方法看起来彼此非常相同

//当您的单元格被选中时,将调用下面的方法

代码语言:javascript
运行
复制
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//Cell selected
}

//当您的单元格被取消选择时,将调用下面的方法。这可能是你的案子

代码语言:javascript
运行
复制
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
//Cell deselected

}

在tableview中选择单元格时,tableview会触发对所选单元格的取消选择方法(多选开启时除外)

希望这将证明你的问题的答案是正确的。

票数 0
EN

Stack Overflow用户

发布于 2016-12-14 16:18:04

首先,您需要学习如何使用segue在视图控制器之间传输数据。这是在View Controller之间传输数据的一种非常快而且很好的方式。还有另一种方式,比如Delegate。

在您当前的视图控制器上,请声明

代码语言:javascript
运行
复制
var selectedCD : Int = 0

在您的didSelectRowAtIndexPath方法中,请修复这些问题。

代码语言:javascript
运行
复制
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()

代码语言:javascript
运行
复制
self.hidesBottomBarWhenPushed = true

然后在执行UITableView委托方法的位置创建以下段方法,

代码语言:javascript
运行
复制
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.上执行使用

的处理的实现

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41135041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档