ios - 基于字符串数组 (Swift) 在 UITableView 中的 viewdidload 上选择单元格

标签 ios swift uitableview core-data swift2

如何根据核心数据数组中的匹配字符串值在 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/

相关文章:

iphone - 以编程方式从 iPhone 应用程序访问设备音乐文件

ios - 设置在 uitableview 单元格中添加的 uilabel 文本

ios - iOS 中的核心图

swift - 如何在 Xcode 中复制命令行工具项目的包资源?

iOS PDFKit不会写

iOS:识别捏合手势但不识别两个手指平移?

uitableview - ios7 页脚和页眉的正确字体样式(大小和 alpha)

ios - Swift 中新添加的 UITableView 单元格的行为不一致

ios - 为什么iphone获取网络定位位置这么快?

ios - 在 Swift 中从 UISplitViewDetail 创建委托(delegate)到 master 时出错