当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Set checkmark for static table view cell

问题描述:

I have an iPhone app with iOS 8 and Swift.

I would like to set the checkmark accessory in a static table view with 3 rows. I use NSUserdefaults where I save a string. For the checkmark I use this code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var identifier = defaults.objectForKey("Sort") as! String

let cell = tableView.dequeueReusableCellWithIdentifier("\(identifier)") as! UITableViewCell

cell.accessoryType = UITableViewCellAccessoryType.Checkmark

return cell

}

But it doesn't work. I always get the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

on this line:

let cell = tableView.dequeueReusableCellWithIdentifier(identifier) as! UITableViewCell

I did not forget to set the cell identifier in the storyboard.

网友答案:

Since you are using static cells, in which case you only have 3, you could set up IBOutlet in storyboard.

@IBOutlet weak var cell1: UITableViewCell!
@IBOutlet weak var cell2: UITableViewCell!
@IBOutlet weak var cell3: UITableViewCell!

Subsequently, while you are still in storyboard, please set up tags for each cell, by clicking on the Attributes Inspector on the right.

For test, I set up the tags from 0 - 2 (i.e., 0, 1, 2).

I am guessing that you are saving a String value in you main controller, and once clicked, you would like so that the corresponding cell would have a checkmark by getting that value out of NSDefault again.

You could do this in method (Swift-wise: function):

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) 

Here is what I came up as a quick test:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{

    NSUserDefaults.standardUserDefaults().setObject("0", forKey: "Sort")
    NSUserDefaults.standardUserDefaults().synchronize()


    let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var aString = defaults.valueForKey("Sort") as? String

    let cellIndex = aString?.toInt()

    if cell1.tag == cellIndex
    {
        cell1.textLabel!.text = "Hallo, I am cell 1"
        cell1.accessoryType = UITableViewCellAccessoryType.Checkmark
    }
    else if cell2.tag == cellIndex
    {
        cell2.textLabel!.text = "Hello, I am cell 2"
        cell2.accessoryType = UITableViewCellAccessoryType.Checkmark
    }
    else if cell3.tag == cellIndex
    {
        cell3.textLabel!.text = "Hello, I am cell 3"
        cell3.accessoryType = UITableViewCellAccessoryType.Checkmark
    }

}

As test, I set it as 0 and get it back immediately from NSDefault; and the first cell is checked with a checkmark.

网友答案:

If you use static table view cells, tableView.dequeueReusableCellWithIdentifier will always return nil. This causes the error.

As an alternative, you can set the checkmars in the viewWillAppear function. There, you can access the cells via the tableView.cellForRowAtIndexPath function:

override func viewWillAppear(_ animated: bool) {
    super.viewWillAppear(animated)

    let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as UITableViewCell
    cell.accessoryType = UITableViewCellAccessoryType.Checkmark

    ...
}

(replace the values of the forRow and inSection parameters as you need)

分享给朋友:
您可能感兴趣的文章:
随机阅读: