2015-04-03 72 views
16

我正在做一個基本的測驗應用程序。選擇是以表格的形式。如果用戶移動到上一個問題,我想向他展示他先前選擇的選項。我已經將選擇存儲在NSUserDefaults中。我如何將這個int值轉換爲NSIndexpath並在我的tableview中使用它?如何將int轉換爲nsindexpath?

回答

50

NSINdexPath UIKit Additions

在你的情況,你可以使用(Swift):

let indexPath = NSIndexPath(forRow: 0, inSection: 0) 

雨燕3.0:

let indexPath = IndexPath(row: 0, section: 0) 
4

This post

重複我複製這裏

在收到回答對於int指數:

NSIndexPath *path = [NSIndexPath indexPathWithIndex:index]; 

創建的項目的索引在節點0到指向具體根據參考。

要在一個UITableView使用indexPath,更合適的方法是

NSIndexPath *path = [NSIndexPath indexPathForRow:yourInt inSection:section]; 
+3

這是的ObjectiveC,而不是斯威夫特 – zisoft 2015-04-03 07:42:18

+1

很公平,但它的工作原理是相同的。您可以使用swift代碼版本向我的帖子建議編輯。我打賭有一個相同的方法;) – 2015-04-03 07:43:25

+1

他不妨發表他自己的回答。 – Josh 2015-06-16 15:28:00

0

你可以indexpath使用下面的代碼

NSIndexPath *index = [NSIndexPath indexPathForRow:YOURINTSTORED inSection:0]; 
+1

這是ObjectiveC,不是Swift – zisoft 2015-04-03 07:41:51