ячейка табличного представления показывает две текстовые метки в одной строке - PullRequest
0 голосов
/ 23 декабря 2018

Я реализую представление свертывания / развертывания таблицы.В части заголовка я хочу, чтобы текст был слева, а значок - справа.

Вот снимок экрана того, как он выглядит в настоящее время в развернутом виде без текста слева.

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
}

есть ли способ показать две текстовые метки в ячейке?Текст слева и атрибутивный текст с изображением справа, как на картинке?

Я мог бы добавить текст к атрибутивному тексту, но затем они слиплись, и текст не центрировался вертикально.

Есть ли способ сделать это?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...