获取节索引路径和编辑节 header

Getting section index path and editing section header

我只是给你一个 short-explanation 我的问题。我有一个文本字段和一个按钮以及一个分组的 table 视图。每当我想触摸我的按钮时,我都想获取文本字段的文本并在 table 中创建一个部分,并将文本分配给刚刚创建的部分。我知道我们可以用 IndexPath 连续处理这个问题,但还没有找到解决方案。谢谢。

这是我为行(有效)和部分尝试的代码:

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

    }

创建数组:

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()
    }