我试图得到一个新的行在底部,如“插入新的单元格”与加号按钮在UITableView的左侧,当用户单击编辑按钮,当用户单击完成按钮时消失。现在,我在表视图的最后一行获得了加号按钮,但我不能动态添加新行。有人能帮上忙吗?我该怎么做,或者他们有什么更好的办法吗?
我在堆栈溢出Using insert rows in a UITableView上找到了这个答案,但我得不到
NSMutableArray* paths = [[NSMutableArray alloc] init];
这句话的用处。
这是我的代码
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.tableView.editing == true {
return nameArray.count + 1
}
else
{
return nameArray.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
if indexPath.row >= nameArray.count
{
cell.textLabel?.text = "insert new row"
}
else
{
cell.textLabel?.text = nameArray[indexPath.row]
}
cell.showsReorderControl = true
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
nameArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
else if editingStyle == .Insert{
self.performSegueWithIdentifier("insertNameSegue", sender:nil)
}
}
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(true, animated: true)
self.tableView.setEditing(true, animated: true)
tableView.reloadData()
}
发布于 2015-04-03 14:48:02
这是目标C:
NSMutableArray* paths = [[NSMutableArray alloc] init];
对于Swift,您应该使用Array
var paths = Array<NSIndexPath>()
paths.append(NSIndexPath(10, 0))
...
https://stackoverflow.com/questions/29406203
复制相似问题