【发布时间】:2015-05-04 10:46:27
【问题描述】:
我有一个动态的 tableView。 TableViewCell 原型如下所示:
“------”
标签
分段控制(是 | 否)
文本视图
“--------”
我的问题是如何隐藏然后显示 tableView 可以自动调整其单元格高度的 textView
这是我的 tableViewCell 类:
class QuestionTableViewCell: UITableViewCell {
@IBOutlet weak var question: UILabel!
@IBOutlet weak var answer: UISegmentedControl!
@IBOutlet weak var comment: UITextView!
这是我的 tableView cellForRowAtIndex:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell", forIndexPath: indexPath) as! QuestionTableViewCell
cell.question.text = "\(indexPath.row + 1). " + questions[indexPath.row]
// check answer and set it to segmentedControl and set color
if questionsAnswers[indexPath.row] == "YES" {
cell.answer.selectedSegmentIndex = 0
cell.answer.tintColor = UIColor(hex: 0x42DC5A)
} else {
cell.answer.selectedSegmentIndex = 1
cell.answer.tintColor = UIColor(hex: 0xD9393E)
}
if cell.comment != nil {
cell.comment.delegate = self
cell.comment.text = "Add a note if necessary.."
cell.comment.textColor = UIColor.lightGrayColor()
}
if questionsAnswers[indexPath.row] == "YES" {
// here i have to hide cell.comment and its space
} else {
// here i have to show it if its possible
}
// pass indexPath.row to action
cell.answer.tag = indexPath.row
cell.answer.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged)
return cell
}
// fires when user taps on segmentedControl
func valueChanged(sender: UISegmentedControl){
// switch segment and update questionsAnswers array
switch sender.selectedSegmentIndex {
case 0:
questionsAnswers[sender.tag] = "YES"
sender.tintColor = UIColor(hex: 0x42DC5A)
case 1:
questionsAnswers[sender.tag] = "NO"
sender.tintColor = UIColor(hex: 0xD9393E)
default:
break
}
}
questions - 包含问题文本的数组
questionsAnswers - 当我从分段控件中保存值时的数组
对不起,如果我的问题不好,我是 ios 编程的新手(
【问题讨论】:
-
为 textView 创建高度约束,并根据偏好将其设置为零或某个值。
标签: ios uitableview swift