2015-04-04 44 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

相關問題