空水平表格单元格

时间:2015-01-10 20:35:21

标签: ios uitableview swift

我一直关注(并转换为swift)Ray Wenderlich教程(link to tutorial),了解如何与水平表建立接口。我的应用程序加载但未在单元格中显示任何数据,如下所示:

iOS simulator

项目中有三个swift文件:
ArticleTableViewCell.swift
HorizontalTableViewCell.swift
ArticleListViewController.swift

// ArticleTableViewCell.swift
// HorizontalTables

import UIKit

class ArticleTableViewCell: UITableViewCell {
    var thumbnail: UIImageView!
    var titleLabel: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)

        var frameA: CGRect = CGRectMake(3, 3, 100, 314)
        thumbnail = UIImageView(frame: frameA)
        thumbnail.opaque = true
        self.contentView.addSubview(thumbnail)

        var frameB: CGRect = CGRectMake(0, thumbnail.frame.size.height * 0.632, thumbnail.frame.size.width, thumbnail.frame.size.height * 0.37)
        titleLabel = UILabel(frame: frameB)
        titleLabel.opaque = true
        titleLabel.backgroundColor = UIColor.clearColor()
        titleLabel.textColor = UIColor.blackColor()
        titleLabel.numberOfLines = 2
        titleLabel.font = UIFont(name: "Helvetica Neue", size: 12)
        thumbnail.addSubview(titleLabel)

        self.transform = CGAffineTransformMakeRotation(CGFloat(M_PI) * 0.5)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

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

    func reuseIdentifier() -> String {
        return "ArticleCell"
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

}


// HorizontalTableViewCell.swift
// HorizontalTables

import UIKit

class HorizontalTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
    var horizontalTableView: UITableView!
    var articles: NSArray!

    override init(frame: CGRect) {
        super.init(frame: frame)

        var frameA: CGRect = CGRectMake(0, 0, 106, 320)
        horizontalTableView = UITableView(frame: frameA)
        horizontalTableView.showsVerticalScrollIndicator =  false
        horizontalTableView.showsHorizontalScrollIndicator = false
        horizontalTableView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI) * 0.5)
        horizontalTableView.frame = CGRectMake(0, 0, 320, 106)
        horizontalTableView.rowHeight = 106
        horizontalTableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
        horizontalTableView.separatorColor = UIColor.clearColor()
        horizontalTableView.backgroundColor = UIColor(red: 201/252, green: 235/252, blue: 245/252, alpha: 1)
        horizontalTableView.dataSource = self
        horizontalTableView.delegate = self
        self.addSubview(horizontalTableView)  
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ArticleCell", forIndexPath: indexPath) as ArticleTableViewCell

        let currentArticle: NSDictionary = articles.objectAtIndex(indexPath.row) as NSDictionary

        cell.titleLabel.text = currentArticle.objectForKey("Title") as NSString
        var pics: AnyObject = currentArticle.objectForKey("ImageName")!
        cell.imageView?.image = UIImage(named: "\(pics)")

        return cell
    }
}


// ArticleListViewController.swift
// HorizontalTables

import UIKit

class ArticleListViewController: UITableViewController {
    var articleDictionary: NSDictionary = NSDictionary()

    let kHeadlineSectionHeight: Int = 26
    let kRegularSectionHeight: Int = 26

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.rowHeight = 45

        let path = NSBundle.mainBundle().pathForResource("Articles", ofType: "plist")

        UITableViewHeaderFooterView.appearance().tintColor = UIColor(red: 201/252, green: 235/252, blue: 245/252, alpha: 1)

        articleDictionary = NSDictionary(contentsOfFile: path!)!
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.tableView.backgroundColor = UIColor(red: 201/252, green: 235/252, blue: 245/252, alpha: 1)
        tableView.rowHeight = 106

    }

    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return articleDictionary.allKeys.count
    }

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

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let keys: NSArray = articleDictionary.allKeys
        var sortedCategories: NSArray = keys.sortedArrayUsingSelector("localizedCompare:")

        var categoryName: NSString = sortedCategories.objectAtIndex(section) as NSString

        return categoryName.substringFromIndex(1)
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as HorizontalTableViewCell

        let keys: NSArray = articleDictionary.allKeys

        var sortedCategories: NSArray = keys.sortedArrayUsingSelector("localizedCompare:")

        var categoryName: NSString = sortedCategories.objectAtIndex(indexPath.section) as NSString

        let currentCategory: NSArray = articleDictionary.objectForKey(categoryName) as NSArray

        cell.articles = currentCategory

        if (indexPath.row % 2) == 1 {
            cell.backgroundColor = UIColor(red: 201/252, green: 235/252, blue: 245/252, alpha: 1)  
        } else {
            cell.backgroundColor = UIColor.clearColor()
        }

        return cell
    }
}

为什么我有没有数据的tableView单元格?

4 个答案:

答案 0 :(得分:1)

看起来你没有将你的细胞注册到你的桌面视图。如果您正在使用故事板,则需要在那里输入单元格标识符。如果是XIB,您需要先将类注册到tableview:

tableView.registerClass(ArticleTableViewCell.self, forCellReuseIdentifier: "ArticleCell")

答案 1 :(得分:0)

UITableViewController将被初始化。同时,调用委托方法和数据源方法进行初始化。那一刻,你只能从UITableViewCell原型获得你所拥有的东西。因此,在成功分发数据后,您必须致电reloadData

答案 2 :(得分:0)

问题是您使用的是dequeueReusableCellWithIdentifier。除非您手动执行此操作,否则它甚至不会使用HorizontalTableViewCell(frame:CGRect)来初始化TableViewCell。你应该这样做:

var cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as? HorizontalTableViewCell

if cell == nil {
    cell = HorizontalTableViewCell(frame: CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height))
}

ArticleTableViewCell相同:

var cell = tableView.dequeueReusableCellWithIdentifier("ArticleCell", forIndexPath: indexPath) as? ArticleTableViewCell
if cell == nil {
    cell = ArticleTableViewCell(frame: CGRectMake(0, 0, 106, 106))
}

答案 3 :(得分:0)

您可以尝试在单元格中使用此init函数

initWithStyle:reuseIdentifier: