表格视图单元格在一行中显示两个文本标签

时间:2018-12-23 19:48:58

标签: uitableview swift4

我正在实现折叠/展开表格视图。在标题部分,我想在左侧显示文本,在右侧显示图标。

这是当前屏幕截图的屏幕截图,左侧没有文字。

enter image description here

这是带有所需显示部分的课程。

struct cellData {
var opend = Bool()
var title = String()
var icon = NSMutableAttributedString()
var sectionData = [String]()
var Img: UIImage?

}

extension NSTextAttachment {
func setImageHeight(height: CGFloat) {
    guard let image = image else { return }
    let ratio = image.size.width / image.size.height

    bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: ratio * height, height: height)
}
}

class DropDown: UITableViewController {



var tableViewData = [cellData]()

let screenSize: CGRect = UIScreen.main.bounds
    //let screenWidth = screenSize.width
    let screenHeight = screenSize.height
    let screenWidth = screenSize.width


    var myIntValue = Int(screenWidth)

    let string = String(repeating: "", count: myIntValue)
    let myAttrString = NSAttributedString(string: string)

    let fullString = NSMutableAttributedString(string: "")

    let image1 = NSTextAttachment()
    image1.image = UIImage(named: "pos")

    image1.setImageHeight(height:35)

    let image1String = NSAttributedString(attachment: image1)

   // fullString.append(myAttrString)
    fullString.append(image1String)
    fullString.append(NSAttributedString(string: ""))

    var s = fullString.string

    var mystring = "Acceptance"



    tableViewData = [cellData(opend: false, title: "Acceptance", icon: fullString, sectionData: ["My attention is in the here and now","I let go of trying to control the people and events in m y life", "Everything is unfolding perfectly the way it is suppose to","I trust in the design of the universe", "I accept people for who they are"], Img: UIImage(named:"Acceptance")),
        cellData(opend: false, title: "Acceptance", icon: fullString, sectionData: ["My attention is in the here and now","I let go of trying to control the people and events in m y life", "Everything is unfolding perfectly the way it is suppose to","I trust in the design of the universe", "I accept people for who they are"], Img: UIImage(named:"Loving")),
        cellData(opend: false, title: "Acceptance", icon: fullString, sectionData: ["My attention is in the here and now","I let go of trying to control the people and events in m y life", "Everything is unfolding perfectly the way it is suppose to","I trust in the design of the universe", "I accept people for who they are"], Img: UIImage(named:"Acceptance")),
        cellData(opend: false, title: "Acceptance", icon: fullString, sectionData: ["My attention is in the here and now","I let go of trying to control the people and events in m y life", "Everything is unfolding perfectly the way it is suppose to","I trust in the design of the universe", "I accept people for who they are"], Img: UIImage(named:"Acceptance"))]
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell2")
        else{
            return UITableViewCell()
    }

    //cell.textLabel?.text = tableViewData[indexPath.section].title
  cell.textLabel?.attributedText = tableViewData[indexPath.section].icon
  cell.textLabel?.textAlignment = .right
   // cell.textLabel?.text = tableViewData[indexPath.section].title
  //  cell.textLabel?.textAlignment = .left
    //cell.imageView?.image = tableViewData[indexPath.section].Img

  // cell.imageView?.image = imageWithImage(image: UIImage(named: tableViewData[indexPath.section])!,scaledToSize: CGSize(width: 20, height: 20))

    return cell
}else{
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell2")
        else{
            return UITableViewCell()
    }
    cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row]
   cell.imageView?.image = nil
    return cell
}

是否可以在一个单元格中显示两个textLabel?左侧的文字和保留图片右侧的属性文字(如图片)?

我可以将文本添加到属性文本中,但是随后它们被粘在一起并且文本没有垂直居中。

有没有办法做到这一点?

0 个答案:

没有答案