如何根据核心数据数组中的匹配字符串值在 UITableview 中选择多个单元格?
我已经保存了之前选择的行(作为字符串),但是现在当我再次加载表时,我希望再次选择所有内容。我找不到或想不出任何解决方案。
数组:
var category:[String] = ["News", "Sport", "Weather", "Earth", "Future", "Shop", "TV", "Radio", "Food", "Music", "Travel", "Local", "Nature", "Venue", "Money"]
我尝试找出 selectRowAtIndexPath
但我没有在 Swift 中看到它。
这是我如何从核心数据获取数据
func fetchData(){
let entityDescription = NSEntityDescription.entityForName("UserSettings", inManagedObjectContext: Context!)
let request = NSFetchRequest()
request.entity = entityDescription
var dataObjects: [AnyObject]?
do {
dataObjects = try Context?.executeFetchRequest(request)
} catch let error as NSError {
print(error)
dataObjects = nil
}
//CAN I UPDATE THE TABLE SELECTION FROM THE FETCH?
for result in dataObjects as! [NSManagedObject] {
print(result.valueForKey("favCategory")!)
}
}
更新 我怎样才能让它根据存储在核心数据中的字符串进行选择(在 Viewdidload 上进行多重选择)
let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0); //slecting 0th row with 0th section
self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None);
最佳答案
您无法在 viewDidLoad
上选择单元格,因为 viewDidLoad
中尚未加载 tableView。您应该将 bool 变量存储为数据源中的标志,然后将 cellForRowAtIndexPath:
方法更新为:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! YourCustomCell
let object = self.dataObjects[indexPath.row] as! YourObject
cell.selected = object.isSelected
return cell
}
关于ios - 基于字符串数组 (Swift) 在 UITableView 中的 viewdidload 上选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813885/