将tableviewcontroller中的表更改为常规视图控制器

时间:2014-08-12 19:27:46

标签: ios uitableview swift

很抱歉,如果这里不欢迎这样的问题,但我很感激你的帮助。我现在已经设置了一个功能齐全的表,但我在故事板中使用TableViewController完成了它。我现在意识到我无法使用该项更改视图的大小,并且它不能很好地适合屏幕。我知道我需要使用常规视图控制器并手动添加表视图。我可以做到这一点,但我花了几个小时让桌子正常工作,我害怕打破它。有人可以带我走过两者之间的过渡吗?

以下是storyboard

这是视图控制器代码

import UIKit

class CharacterTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var characterTableView: UITableView!

    var characterArray: [Character] = [Character]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.characterSetup()
    }

    func characterSetup() {

        var admiralSwiggins = Character(name: "Admiral Swiggins", abilities: "Stun, Blind, Shield, Damage Over Time, Area of Effect", characterImage: "admiral.png")
        var ayla = Character(name: "Ayla", abilities: "Area of Effect, Damage Over Time, Fly, Lifesteal, Shield, Slow", characterImage: "ayla.png")
        var clunk = Character(name: "Clunk", abilities: "Lifesteal, Area of effect, Ensnare, Slow, Launch String", characterImage: "clunk.png")

        characterArray.append(admiralSwiggins)
        characterArray.append(ayla)
        characterArray.append(clunk)
    }

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:CharacterTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as CharacterTableViewCell

        if indexPath.row % 2 == 0 {
            cell.backgroundColor = UIColor.redColor()
        }

        let character = characterArray[indexPath.row]

        cell.nameLabel.text = character.name
        cell.abilityLabel.text = character.abilities
        cell.characterImage.image = UIImage(named: character.characterImage)

        return cell
    }

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

    }
}

这是细胞

import UIKit

class CharacterTableViewCell: UITableViewCell {

    @IBOutlet var nameLabel: UILabel!
    @IBOutlet var abilityLabel: UILabel!
    @IBOutlet var characterImage: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    } 
}

0 个答案:

没有答案