小编Ste*_*owe的帖子

如何使用按钮标签在swift中访问自定义单元格的内容?

我有一个应用程序,在自定义单元格中有自定义按钮.如果您选择单元格,它将转移到详细视图,这是完美的.如果我在单元格中选择一个按钮,下面的代码会将单元格索引打印到控制台中.

我需要访问所选单元格的内容(使用按钮)并将它们添加到数组或字典中.我是新手,因此很难找到如何访问单元格的内容.我尝试使用didselectrowatindexpath,但我不知道如何强制索引是标记的索引...

所以基本上,如果有3个单元格中有'Dog','Cat','Bird'作为每个单元格中的cell.repeatLabel.text,我选择第1行和第3行(索引0和2)中的按钮,它应该将'Dog'和'Bird'添加到数组/字典中.

    // MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return postsCollection.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    // Configure the cell...
    var currentRepeat = postsCollection[indexPath.row]
    cell.repeatLabel?.text = currentRepeat.product
    cell.repeatCount?.text = "Repeat: " + String(currentRepeat.currentrepeat) + " of " + String(currentRepeat.totalrepeat)

    cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton

    cell.checkButton.tag = indexPath.row;

    cell.checkButton.addTarget(self, …
Run Code Online (Sandbox Code Playgroud)

tags uibutton uitableview tableviewcell swift

19
推荐指数
3
解决办法
5万
查看次数

标签 统计

swift ×1

tableviewcell ×1

tags ×1

uibutton ×1

uitableview ×1