如何使同一个tableview中有两个单独的单元格组?

时间:2016-08-30 15:45:25

标签: ios swift uitableview uicollectionview

如何使name2与名称不相等时,它会将名称中缺少的字符串添加到表格视图中,但文本样式不同?

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中的灰色?

1 个答案:

答案 0 :(得分:1)

您可以检查model() { return this.store.findAll('category') .then( function(categories) { let data = []; data = categories.map(category => { return { y: category.get('totalGrams'), name: category.get('name') } }); return [{name: 'gear', colorByPoint: true, data}]; }) }, names2是否包含names数组对象,然后更改所需的文字样式。

cellForRowAtIndexPath

修改:我没有正确理解图片,但您可以尝试这样的事情。

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