[英]Getting section index path and editing section header
我只想簡要解釋一下我的問題。 我有一個文本字段和一個按鈕以及一個分組的tableView。 每當我想觸摸我的按鈕時,我想獲取文本字段的文本並在表格中創建一個部分,並將文本分配給剛創建的部分。 我知道我們可以使用IndexPath連續處理它,但是還沒有找到解決方案。 謝謝。
制作一個數組:
var sections: Array <String>!
在initilzer或ViewDidLoad中初始化它
self.sections = Array();
覆蓋這兩種方法
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sections.count;
}
在按鈕操作上,使用以下代碼
@IBAction func addItem(sender: AnyObject) {
self.sections.append(self.textField.text)
self.tableView.reloadData()
}
這是我為行嘗試的代碼(有效)和部分:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = myArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
//Whenever an item is added, every sector will get an identifier. So with that, they can match the array's index.
return nil
}
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]! {
return nil
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.