I have a data like dynamic string.
And I want to check this string is out of my label width which numberOfLines
equal one.
If out of my label width, I use cellA
.
If not out of my label width, I use cellB
.
And my point is how to check and change "isLongName" boolvalue.
Have any suggestion to me?
var isLongName: Bool = false
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// How to check the string is long name (width out of cellB)
if isLongName && indexPath.row == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellA", for: indexPath) as! TableViewCellA
cell.titleLabel.text = itemsArray[indexPath.row].title
cell.subtitleLabel.text = itemsArray[indexPath.row].value
cell.expandBtn.addTarget(self, action: #selector(expandBtnPressed), for: .touchUpInside)
cell.expandBtn.tag = indexPath.row
if isExpand {
cell.expandBtn.setTitle("close", for: .normal)
cell.subtitleLabel.numberOfLines = 0
} else {
cell.expandBtn.setTitle("open", for: .normal)
cell.subtitleLabel.numberOfLines = 1
}
cell.remakeLayout(isExpand: isExpand)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellB", for: indexPath) as! TableViewCellB
cell.titleLabel.text = itemsArray[indexPath.row].title
cell.subtitleLabel.text = itemsArray[indexPath.row].value
return cell
}
}
@objc func expandBtnPressed(sender: UIButton) {
let indexPath = IndexPath(row: sender.tag, section: 0)
UIView.performWithoutAnimation {
tableView.beginUpdates()
self.isExpand = !isExpand
tableView.reloadRows(at: [indexPath], with: .none)
tableView.endUpdates()
}
}