2015-04-04 88 views
0

我在我的tableView中有4个部分,每个部分都有单个单元格。当用户选择第四部分的单元格时,我希望他们键入它。所以我只是将textfield添加为didSelectRow函数中该单元的附件视图,如下所示。添加UITextfield作为单元格的附件视图

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    if indexPath.section == 3 { 

      //adding textField 
      theTextField = UITextField(frame: CGRectMake(10, 480, 300, 40)) 
      theTextField.backgroundColor = UIColor.brownColor() 
      theTextField.placeholder = "Please type here...." 
      theTextField.textColor = UIColor.yellowColor() 
      theTextField.layer.cornerRadius = 10.0 
      selectedCell?.accessoryView = theTextField 
     } 

但是,当我点击它,键盘隐藏该单元格。我想要表格视图向上滚动。请帮我解决这个问题,或者请让我知道是否有任何其他方式来实现这一点!

This is when I select the fourth cell

Table View Look like this

回答

0

不清楚为什么要实现在“didSelectRowAtIndexPath方法”的池规格时,一个更合乎逻辑的地方是“的cellForRowAtIndexPath”。我实现了你的代码,除了在cellForRowAtIndexPath中放置第3节的单元格规格并且表格单元格按预期向上滚动。见下面的代码。要利用tableview滚动,单元格需要在cellRowForIndex中定义。为了满足您在评论中陈述的目标,您可以使用.hidden功能并插入代码,如图所示。

import UIKit 

类:{的UITableViewController

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.section == 3 { 
     selectedCell.accessoryView?.backgroundColor = UIColor.brownColor() 
     selectedCell.accessoryView?.hidden = false 
    } else { 
     selectedCell.accessoryView?.hidden = true 
    } 

} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 4 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 
    if indexPath.section == 3 { 

     //adding textField 
     var theTextField = UITextField(frame: CGRectMake(10, 480, 300, 40)) 
     theTextField.backgroundColor = UIColor.brownColor() 
     theTextField.placeholder = "Please type here...." 
     theTextField.textColor = UIColor.yellowColor() 
     theTextField.layer.cornerRadius = 10.0 
     cell.accessoryView = theTextField 
     cell.accessoryView?.hidden = true 
    } 

    else { 
     cell.textLabel!.text = "\(indexPath.section)" 
    } 

    return cell 
} 

}

Simulator running 4S

+0

感谢您的答复!赛义德我使用了didSelectRow func,因为我正在考虑使用cell.imageView.image将图片放入一个单元格中,并且在用户选择单元格后,我希望它成为文本框。这就是我在didSelectRow func中实现的原因。你有什么想法如何实现? – 2015-04-05 03:08:06

+0

我想了解这一点。 (1)在第3节第0行中,您希望放置一张图片。 (2)当他用户选择你想要显示一个textView的行。我理解正确吗? – 2015-04-05 03:32:59

+0

是的!我可以使用didSelectRow来做到这一点,但只有当键盘出现时,它应该向上滚动并且用户希望看到文本字段......在我的情况下,键盘隐藏该单元格。如果我使用节(0)行(0),一切工作正常,像我需要的。但我想在第(3)节。 – 2015-04-05 03:52:50

相关问题