首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在UITableViewin swift中单击编辑按钮时新建一行

在UITableViewin swift中单击编辑按钮时新建一行
EN

Stack Overflow用户
提问于 2015-04-02 14:17:23
回答 1查看 900关注 0票数 2

我试图得到一个新的行在底部,如“插入新的单元格”与加号按钮在UITableView的左侧,当用户单击编辑按钮,当用户单击完成按钮时消失。现在,我在表视图的最后一行获得了加号按钮,但我不能动态添加新行。有人能帮上忙吗?我该怎么做,或者他们有什么更好的办法吗?

我在堆栈溢出Using insert rows in a UITableView上找到了这个答案,但我得不到

代码语言:javascript
运行
复制
NSMutableArray* paths = [[NSMutableArray alloc] init];

这句话的用处。

这是我的代码

代码语言:javascript
运行
复制
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()

}
EN

回答 1

Stack Overflow用户

发布于 2015-04-03 14:48:02

这是目标C:

代码语言:javascript
运行
复制
NSMutableArray* paths = [[NSMutableArray alloc] init];

对于Swift,您应该使用Array

代码语言:javascript
运行
复制
var paths = Array<NSIndexPath>()
paths.append(NSIndexPath(10, 0))
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29406203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档