Ler Ws Ler Ws - 3 months ago 6
iOS Question

How to make it such that there are two seperate groups of cells in the same tableview?

How do I make it such that when names2 is not equals to names, it will add the missing strings from names into the table view but with a different text style?

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)
}
}

Answer

You can check that names2 is contains names array object inside cellForRowAtIndexPath and then change the text style you want.

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.textColor = UIColor.blackColor() //Set other style that you want
    }
    else {
        cell.textColor = UIColor.redColor() //Set other style that you want
    }
    return cell!
}
Comments