【问题标题】:Getting section index path and editing section header获取节索引路径和编辑节标题
【发布时间】:2015-02-02 09:10:32
【问题描述】:

我将简要解释一下我的问题。我有一个文本字段和一个按钮以及一个分组的 tableView。每当我想触摸我的按钮时,我想获取文本字段的文本并在表格中创建一个部分,并将文本分配给刚刚创建的部分。我知道我们可以使用 IndexPath 连续处理这个问题,但还没有找到解决方案。谢谢。

【问题讨论】:

  • 到目前为止你尝试了什么(显示代码)并详细解释它做错了什么。
  • 代码在哪里???我什么都做不了!!!
  • 我添加了,请耐心等待:D

标签: ios iphone swift tableview


【解决方案1】:

制作一个数组:

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

【讨论】:

  • 非常感谢。我有一个问题,func前面的“覆盖”实际上是做什么的?
  • 我意识到,我真正忘记的唯一一件事就是在我的第一个代码上附加:(。谢谢!
  • 如果你喜欢我的回答,请投票 :) @MertKarakus
  • 好吧,我的代表已经够多了。你是幸运的先生:D
【解决方案2】:

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

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

    }

【讨论】:

  • 在另一个注意事项中,您不应该在每次调用 cellForRowAtIndexPath 时都使单元格成双列。将 UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 替换为 tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 作为 UITableViewCell
  • 确切地说有什么不同? @AbubakrDar
  • 看看这个答案,希望你能理解stackoverflow.com/questions/2928873/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多