自定义标签文本不会出现在UITableViewCell上

时间:2017-06-08 15:27:43

标签: ios swift swift3

我有一个包含两个不同原型单元格的表视图。

第二个原型单元格有一个自定义标签,但是当我执行项目时它没有显示任何内容。文本在那里(我知道因为它正在打印)但它没有出现。

TableView Storyboard

Simulator: Image cell ok, but text cell is empty

- > TableViewController:

import UIKit

导入Twitter

类TweetMentionsTableViewController:UITableViewController {

lazy var images: [MediaItem] = []
lazy var userMentions: [Mention]? = []
lazy var hashtags: [Mention]? = []
lazy var urls: [Mention]? = []

var tweet: Twitter.Tweet?

private enum MentionTypes {
    case Images
    case Hashtags
    case Users
    case URLs
}

private var mentionsCollection: Dictionary<MentionTypes, Bool> = [
    MentionTypes.Images: false,
    MentionTypes.Hashtags: false,
    MentionTypes.Users: false,
    MentionTypes.URLs: false
]

private var sectionTypes: Dictionary<MentionTypes, Int?> = [
    MentionTypes.Images: nil,
    MentionTypes.Hashtags: nil,
    MentionTypes.Users: nil,
    MentionTypes.URLs: nil
]

override func numberOfSections(in tableView: UITableView) -> Int {
    var count = 0
    if !(tweet?.media.isEmpty)! {
        mentionsCollection[MentionTypes.Images] = true
        count += 1
    }
    if !(tweet?.hashtags.isEmpty)! {
        mentionsCollection[MentionTypes.Hashtags] = true
        count += 1
    }
    if !(tweet?.userMentions.isEmpty)! {
        mentionsCollection[MentionTypes.Users] = true
        count += 1
    }
    if !(tweet?.urls.isEmpty)! {
        mentionsCollection[MentionTypes.URLs] = true
        count += 1
    }
    return count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if mentionsCollection[MentionTypes.Images] == true {
            sectionTypes[MentionTypes.Images] = section
            mentionsCollection[MentionTypes.Images] = false
            return (tweet?.media.count)!
        } else if mentionsCollection[MentionTypes.Hashtags] == true {
            sectionTypes[MentionTypes.Hashtags] = section
            mentionsCollection[MentionTypes.Hashtags] = false
            return (tweet?.hashtags.count)!
        } else if mentionsCollection[MentionTypes.Users] == true {
            sectionTypes[MentionTypes.Users] = section
            mentionsCollection[MentionTypes.Users] = false
            return (tweet?.userMentions.count)!
        } else if mentionsCollection[MentionTypes.URLs] == true {
            sectionTypes[MentionTypes.URLs] = section
            mentionsCollection[MentionTypes.URLs] = false
            return (tweet?.urls.count)!
        }
    return 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //let data = temp[indexPath.row][indexPath.section]
    let cell = tableView.dequeueReusableCell(withIdentifier: "TweetMention", for: indexPath)
    // Configure the cell...
    if let sec = sectionTypes[MentionTypes.Images], indexPath.section == sec {
        let image = tweet?.media[indexPath.row].url
        if let tweetMentionCell = cell as? TweetMentionsTableViewCell {
            tweetMentionCell.imageURL = image!
            return cell
        }
    }

   let cell2 = tableView.dequeueReusableCell(withIdentifier: "TweetMention2Cell", for: indexPath)

    if let sec = sectionTypes[MentionTypes.Hashtags], indexPath.section == sec {
        let hashtag = tweet?.hashtags[indexPath.row]
        if let tweetMentionCell = cell2 as? TweetMentions2TableViewCell {
            tweetMentionCell.hashtag = (hashtag?.keyword)!
        }
    } else if let sec = sectionTypes[MentionTypes.Users], indexPath.section == sec {
        let userMention = tweet?.userMentions[indexPath.row]
        if let tweetMentionCell = cell2 as? TweetMentions2TableViewCell {
            tweetMentionCell.userMention = (userMention?.keyword)!
        }
    } else if let sec = sectionTypes[MentionTypes.URLs], indexPath.section == sec {
        let url = tweet?.urls[indexPath.row]
        if let tweetMentionCell = cell2 as? TweetMentions2TableViewCell {
            tweetMentionCell.url = (url?.keyword)!
        }
    }

    return cell2
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if let sec = sectionTypes[MentionTypes.Images], section == sec {
        return "Images"
    } else if let sec = sectionTypes[MentionTypes.Hashtags], section == sec {
        return "Hashtags"
    } else if  let sec = sectionTypes[MentionTypes.Users], section == sec {
        return "Users"
    } else if let sec = sectionTypes[MentionTypes.URLs], section == sec {
        return "URLs"
    }
    return nil
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let cell = sender as? TweetMentionsTableViewCell {
        if segue.identifier == "Show Image" {
            if let imageVC = segue.destination as? ImageViewController {
                imageVC.imageURL = cell.imageURL?.absoluteURL
                //imageVC.imageRatio = tweet?.media[0].aspectRatio
            }
        }
    }
        if let cell = sender as? TweetMentions2TableViewCell {
            if segue.identifier == "Show Text" {
                if let mentionsSTTVC = segue.destination as? MentionsSearchTweetTableViewController {
                    if let text = cell.mentLabel {
                        mentionsSTTVC.mentionToSearch = text.text!
                    }
                }
            }
    }
}

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if let cell = sender as? TweetMentions2TableViewCell {
        if (cell.url.contains("http")) {
            UIApplication.shared.open(NSURL(string: cell.url)! as URL)
            return false
        }
    }
    return true
}

}

- &GT; TableViewCell:

import UIKit

class TweetMentions2TableViewCell:UITableViewCell {

@IBOutlet weak var mentLabel: UILabel!

var userMention = "" {
    didSet {
        updateUI(mentionType: 2)
    }
}
var hashtag = "" {
    didSet {
        updateUI(mentionType: 1)
    }
}
var url = "" {
    didSet {
        updateUI(mentionType: 3)
    }
}

func updateUI(mentionType: Int) {
    if mentionType == 1 {
        mentLabel.text = hashtag
    } else if mentionType == 2 {
        mentLabel.text = userMention
    } else if mentionType == 3 {
        mentLabel.text = url
    }
    print(mentLabel.text!)
}

}

0 个答案:

没有答案