ios - detailTextLabel 未显示,但已设置 100%

标签 ios xcode swift uitableview custom-cell

我有一个 MGSwipeTableCell像这样添加到我的 tableView 中:

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

    let reuseIdentifier = "Cell"
    var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
    if cell == nil {

        cell = MGSwipeTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
    }
    cell.delegate = self
    cell.backgroundColor = blue
}

一切正常。但是现在,如果我点击单元格,单元格的颜色可能会先变为白色,然后变为绿色(默认颜色为蓝色)。这是按下单元格时应该发生的情况:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  let mySelectedCell = tableView.cellForRowAtIndexPath(indexPath) as! MGSwipeTableCell!
  mySelectedCell.backgroundColor = green
  mySelectedCell.detailTextLabel?.text = "Test" 
}

所以颜色应该只变成绿色而不是白色,然后变成绿色,并且应该显示带有文本“Test”的 detailTextLabel。 我希望你能帮我解决这个问题。我真的不知道该怎么办。

解决方法:

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

let reuseIdentifier = "Cell"
    var cell =  tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
    cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle,  reuseIdentifier: reuseIdentifier)
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.delegate = self
    cell.backgroundColor = blue

}

最佳答案

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

let reuseIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil {
    //Change this line
    cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
//And this line
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.delegate = self
cell.backgroundColor = blue
}

保留“didSelectRowAtIndexPath”中的所有内容

关于ios - detailTextLabel 未显示,但已设置 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33742184/

相关文章:

ios - 在 Swift 中进行应用内购买后,广告不会被删除

xcode - XCode 9.1模拟器导致macOS系统崩溃

ios - 并发(dispatchqueue)测试未按预期进行 ios/swift

ios - UiTableView 生成的单元格数量

ios - Xcode7.1 代码不能用证书签名吗?

iphone - 我必须使用 iOS 应用程序内购买来支付每月可变金额的服务账单吗?

ios - SwiftUI Spacer 无法在 HStack 内的 Toggle 旁边工作

ios - 如何防止访问其他类的委托(delegate)方法?

xcode - Xcode 11/iOS 13 没有控制台输出

swift - UITextField rightView 图像和按钮?