2015-05-04 100 views
0

我有一个动态tableView。 TableViewCell原型是这样的:ios swift在textView单元格中显示/隐藏元素并清除其空间

“------------------”
标签
segmentedControl(是|否)
的TextView
“--- ---------------”

我的问题是如何隐藏,然后显示的TextView是的tableView可以自动调整其细胞高度

这里是我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 
    } 
} 

问题 - 有问题
questionsAnswers的文本数组 - 当我保存从分段控制值的数组

很抱歉,如果我的问题是坏,我'在ios编程中的新手(

+1

为textView创建高度约束,并根据偏好将其设置为零或某个值。 – Sandeep

回答

1

在界面构建器中打开xib。然后按Ctrl +将约束拖动到该视图的UIViewController。

对于您的情况,您将需要textView的高度以及segmentedControl或底部布局之间的间距。

之后,只需设置您的heightConstraint.constant = 0spacingConstraint.constant = 0

如果您只设置高度1,则在segmentedControl和底部布局之间会有两个间距,如其他答案中的建议。

如果需要再次显示它,只是一定要保存的初始值的约束常量,然后设置回这样heightConstraint.constant = initialHeightspacingConstraint.constant = initialSpacing

+0

ty,它解决了我的问题! – mikle94

0

如果您使用的是自动布局,您可以在文本视图中添加高度约束,并在您的单元格子类中添加IBOutlet。如果您想隐藏它,只需将myConstraint.constant设置为零即可。

0

谢谢你们,我创建了一个IBOutlet在TextView中的高度约束我tableViewCell类:

@IBOutlet weak var heightConstraint: NSLayoutConstraint! 

然后,我将这段代码:

if questionsAnswers[indexPath.row] == "YES" { 
    cell.heightConstraint.constant = 0.0 
} else { 
    cell.heightConstraint.constant = 70.0 
} 

它效果很好,但我在调试器中得到这个:

大概在以下列表中的约束条件中的至少一个是你不想要一个 。尝试这一点:(1)看看每个约束,并尝试找出你不期望的 ; (2)找到添加了不想要的约束或约束并修复它的代码。(注意:如果你看到 NSAutoresizingMaskLayoutConstraints,你不明白,请参阅 到文档中的UIView财产 translatesAutoresizingMaskIntoConstraints)

"<NSLayoutConstraint:0x7f9fc2f45300 V:[UITextView:0x7f9fc3017a00'Add a note if necessary..'(70)]>", 
"<NSLayoutConstraint:0x7f9fc2df4460 UILabel:0x7f9fc2f40410'3. Question sadsadas das ...'.top == UITableViewCellContentView:0x7f9fc2f402d0.topMargin>", 
"<NSLayoutConstraint:0x7f9fc2df4550 V:[UILabel:0x7f9fc2f40410'3. Question sadsadas das ...']-(NSSpace(8))-[UISegmentedControl:0x7f9fc2f40e20]>", 
"<NSLayoutConstraint:0x7f9fc2df4700 UITextView:0x7f9fc3017a00'Add a note if necessary..'.bottom == UITableViewCellContentView:0x7f9fc2f402d0.bottomMargin>", 
"<NSLayoutConstraint:0x7f9fc2df4750 V:[UISegmentedControl:0x7f9fc2f40e20]-(NSSpace(8))-[UITextView:0x7f9fc3017a00'Add a note if necessary..']>", 
"<NSLayoutConstraint:0x7f9fc2ed5060 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f9fc2f402d0(81.5)]>") 

我应该改变一些限制? 还有一个问题,如果用户点击“否”segmentedControl我想向用户显示textView。
我可以做这样的:

// pass indexPath.row to action 
cell.answer.tag = indexPath.row 
cell.answer.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged) 

// 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) 
     tableView.reloadData() 
    case 1: 
     questionsAnswers[sender.tag] = "NO" 
     sender.tintColor = UIColor(hex: 0xD9393E) 
     tableView.reloadData() 
    default: 
     break 
    } 
} 

新questionsAnswers阵列只需重新加载的tableView?

+0

对于第二部分,是的。你只需要更新你的模型,然后重新加载表格视图。 –