iOS - Swift - 自定义TableView - 返回多个原型

时间:2015-03-27 23:21:53

标签: ios uitableview swift

这适用于自定义UITableView。我想基于堆栈上的内容返回一个原型单元格,由数组MentionItemsStack表示。这段代码有什么概念上的错误吗?自定义UITableView上没有任何内容。

class MentionsTableViewController:UITableViewController {

var mentions: Tweet? {
    didSet {
        placeMentionItemsInStack()
    }
}

var MentionItemsStack = [String?]()

private struct Storyboard {
    static let imagesCellReuseIdentifier = "imagesCell"
    static let urlsCellReuseIdentifier = "urlsCell"
    static let hashtagsCellResuseIdentifier = "hashtagsCell"
    static let usersCellReuseIdentifier = "userMentionsCell"
    static let emptyCellReuseIdentifier = "emptyCell"
}

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

func placeMentionItemsInStack() {
    if !mentions!.userMentions.isEmpty {
        MentionItemsStack.append("Users")
    }
    if !mentions!.hashtags.isEmpty {
        MentionItemsStack.append("Hashtags")
    }
    if !mentions!.urls.isEmpty {
        MentionItemsStack.append("URLs")
    }
    if !mentions!.media.isEmpty {
        MentionItemsStack.append("Images")
    }
}

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1 // returned 1 for now?
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let emptyCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.emptyCellReuseIdentifier, forIndexPath: indexPath) as EmptyTableViewCell

    if !MentionItemsStack.isEmpty {
        if let identifier = MentionItemsStack.removeLast() {
            switch identifier {
                case "Users":
                    let userMentionCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.usersCellReuseIdentifier, forIndexPath: indexPath) as UsersTableViewCell
                    userMentionCell.tweet = mentions
                    return userMentionCell

                case "Hashtags":
                    let hashtagCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.hashtagsCellResuseIdentifier, forIndexPath: indexPath) as HashtagsTableViewCell
                    hashtagCell.tweet = mentions
                    return hashtagCell

                case "URLs":
                    let urlCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.urlsCellReuseIdentifier, forIndexPath: indexPath) as URLsTableViewCell
                    urlCell.tweet = mentions
                    return urlCell

                case "Images":
                    let imageCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.imagesCellReuseIdentifier, forIndexPath: indexPath) as ImagesTableViewCell
                    imageCell.tweet = mentions
                    return imageCell

                default: break
            }
        }
    }
    return emptyCell // arbitrary return statement
} 
}

0 个答案:

没有答案