Skip to main content
 首页 » 操作系统

ios之获取节索引路径和编辑节标题

2024年01月01日46kenshinobiy

我将简要解释一下我的问题。我有一个文本字段和一个按钮以及一个分组的 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() 
    }