cellForRowAtIndexPath为自定义单元格类返回nil

时间:2015-01-11 22:54:21

标签: ios uitableview swift

我为我的应用程序中使用自定义单元格的另一个表视图执行了完全相同的方法,但是当我尝试强制转换单元格时,当我将其作为自定义类进行转换时,它返回nil,但是当我执行它时它不会返回nil不要试图抛出细胞。我似乎正在做正确的事我注册了类以便重复使用单元格。

//Register class for cell reuse
    self.table.registerClass(ChatTableViewCell.classForCoder(), forCellReuseIdentifier: cellId)

func changeTextForCell(str: String, indexPath: NSIndexPath) {
    var cell: ChatTableViewCell! = self.table.cellForRowAtIndexPath(indexPath) as ChatTableViewCell
}

^^为单元格返回nil

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell : ChatTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? ChatTableViewCell

    if cell == nil {
        cell = ChatTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellId)
    }

    changeTextForCell("something", indexPath: indexPath)

    cell!.selectionStyle = UITableViewCellSelectionStyle.None

    if self.messages[indexPath.row].sender == PFUser.currentUser().username {
        cell!.contentView.backgroundColor = UIColor(red: 0, green: 0.737, blue: 0.831, alpha: 1)
        cell!.userImageView.image = UIImage()
        cell!.userImageView.layer.borderWidth = 0
        cell!.chatText.textAlignment = NSTextAlignment.Right
    }

    if self.messages[indexPath.row].sender != PFUser.currentUser().username {
        cell!.contentView.backgroundColor = UIColor(red: 1, green: 0.251, blue: 0.506, alpha: 1)
        cell!.userImageView.image = UIImage.imageWithImage(UIImage(named: "snorlax.png"), newSize: CGSizeMake(cell!.height!, cell!.height!))
        cell!.userImageView.layer.borderWidth = 2
        cell!.userImageView.layer.borderColor = UIColor.whiteColor().CGColor
        cell!.chatText.textAlignment = NSTextAlignment.Left
    }

    cell!.chatText.text = "\(self.messages[indexPath.row].text)"

    return cell!
}


class ChatTableViewCell: UITableViewCell {
var chatText: UILabel!
var userImageView: UIImageView!
var height: CGFloat?

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    var size = self.contentView.frame.size

    self.chatText = UILabel(frame: CGRectMake(55, 14, 257, 21))
    self.chatText.lineBreakMode = NSLineBreakMode.ByWordWrapping
    self.chatText.numberOfLines = 0
    self.chatText.textColor = UIColor.whiteColor()
    self.chatText.font = UIFont.systemFontOfSize(14)
    self.chatText.textAlignment = NSTextAlignment.Left
    self.chatText.autoresizingMask = (UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight)
    self.contentView.addSubview(self.chatText)

    self.height = 39
    self.userImageView = UIImageView(frame: CGRectMake(8, 5, self.height!, self.height!))
    self.userImageView.clipsToBounds = true
    self.userImageView.layer.masksToBounds = true
    self.userImageView.layer.cornerRadius = self.height! / 2
    self.contentView.addSubview(userImageView)
}

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

}

0 个答案:

没有答案