2016-12-14 55 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

相關問題