如何使names2不等于names时,将names中缺少的字符串添加到表视图中,但使用不同的文本样式?

import UIKit

class TableViewController: UITableViewController {

    var names = [String]()
    var identities = [String]()
    var names2 = [String]()

    override func viewDidLoad() {

        names = ["First", "Second", "Third", "Fourth"]
        identities = ["A", "B", "C", "D"]
        names2 = ["First", "Second"]
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    }

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


        let cell = tableView.dequeueReusableCellWithIdentifier("cell")

        cell?.textLabel!.text = names[indexPath.row]



        return cell!
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let vc = identities[indexPath.row]
        let viewController = storyboard?.instantiateViewControllerWithIdentifier(vc)
        self.navigationController?.pushViewController(viewController!, animated: true)
    }
}

对于集合视图中的图像
import UIKit

class MedalViewController: UICollectionViewController {

    var imagesArray = [String]()
    var identities = [String]()
    var identities2 = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        imagesArray = ["1", "2", "3"]
        identities = ["Shield", "Tie", "Star"]
        identities2 = ["Shield", "Tie"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)

        let imageView = cell.viewWithTag(1) as! UIImageView
        imageView.image = UIImage(named: imagesArray[indexPath.row])

        return cell

    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imagesArray.count
    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let vc = identities[indexPath.row]
        let viewController = storyboard?.instantiateViewControllerWithIdentifier(vc)
        self.navigationController?.pushViewController(viewController!, animated: true)
        viewController?.title = self.identities[indexPath.row]
    }

}

我如何使它使得在本例中丢失的标识符“Star”(其图像为“3”)在collectionsView中变灰?

最佳答案

您可以检查names2是否包含names数组对象,然后更改所需的文本样式。

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

    let cell = tableView.dequeueReusableCellWithIdentifier("cell")
    cell?.textLabel!.text = names[indexPath.row]
    if (names2.contains(names[indexPath.row])) {
        cell.textLabel.textColor = UIColor.blackColor() //Set other style that you want
    }
    else {
        cell.textLabel.textColor = UIColor.redColor() //Set other style that you want
    }
    return cell!
}

编辑:我不太了解图片,但你可以试试这样的东西。
if (identities2.contains(identities[indexPath.row])) {
    cell.imageView = UIImage(named: identities[indexPath.row])
}
else {
    cell.imageView = UIImage(named: "DefaultGrayImage") //Set default image not in identities2
}

关于ios - 如何使其在同一tableview中具有两组独立的单元格?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39231952/

10-13 09:27