使用swift更改uitableView特定单元格的标签文本

时间:2019-02-21 06:09:28

标签: ios swift uitableview

我在选定的单元格上应显示标签“正在播放”和“图像”,然后当我选择其他单元格时,所选的上一个应该立即清除。

我写的代码:

func didSelectedRow(indexPath: IndexPath) {
    guard let cell = tblMusicPro.cellForRow(at: indexPath) as? musicTableViewCell else {
        return
    }
    if indexPath == currentIndex {
        cell.lblTagsPro.text = "Now Playing"
        cell.imgPlaying.alpha = 1
    } else {
        cell.lblTagsPro.text = ""
        cell.imgPlaying.alpha = 0
    }
}

'Now Playing' and 'Image' should be display on selected cell, then as i select other cell the prev selected should be clear now.

3 个答案:

答案 0 :(得分:1)

您已经更改了表格单元格的对象状态,但是直到重新加载表格单元格后,该状态才会反映出来。您可以重新加载整个tableView,也可以仅重新加载两个正在取消选择的单元格。

为此,

在您的课程中保留一个变量以存储选定的单元格索引

var selectedCell: IndexPath?

单击单元格时,将indexpath存储在此变量中并重新加载该单元格。说,

func selectCell(indexPath: IndexPath) {
    selectedCell = indexPath
    self.tblMusicPro.reloadData()
}

在您的cellForRowAtIndexPath中,检查是否已选择单元格?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: <cell_identifier>, for: indexPath) as! musicTableViewCell
    if indexPath == selectedCell {
        cell.lblTagsPro.text = "Now Playing"
        cell.imgPlaying.alpha = 1
    } else {
        cell.lblTagsPro.text = ""
        cell.imgPlaying.alpha = 0
    }

    //
    // Rest of your implementation
    //

    return cell
}

答案 1 :(得分:1)

有一些解决方案。

  1. 实现TableView委托方法didDeselectRowAt

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

  2. 如果您有一个自定义单元格类,请在该单元格类的setSelected方法中指定选择/取消选择逻辑。

    override func setSelected(_ selected: Bool, animated: Bool)

  3. 如果具有以前选择的索引路径,则设置标签属性。从didSelectRowAt委托方法中调用此方法。

    func removeNowPlayingText() {
         if let cell = tableView.cellForRow(at: previouslySelectedIndexPath) as? MusicTableViewCell {
              cell.lblTagsPro.text = ""
              cell.imgPlaying.alpha = 0
         }
    }
    

答案 2 :(得分:1)

为此,请使用一个数组存储 IndexPath ,然后按照此代码在cellForRowAt处进行检查,在选择单元格时将IndexPath添加到数组中,如果已经存在则将其删除在数组中,然后重新加载tableview单元以更新值。

var selectedCells:[IndexPath] = []

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tblMusicPro.cellForRow(at: indexPath) as? musicTableViewCell else {
            return
        }
        if selectedCells.contains(indexPath) {
            cell.lblTagsPro.text = "Now Playing"
            cell.imgPlaying.alpha = 1
        } else {
            cell.lblTagsPro.text = ""
            cell.imgPlaying.alpha = 0
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if selectedCells.contains(indexPath) {
            let getIndex = selectedCells.index(of: indexPath)
            selectedCells.remove(at: getIndex)
        }else{
            selectedCells.append(indexPath)
        }

        tableView.reloadRows(at: [indexPath], with: true)

    }