首页 > 解决方案 > 如何正确地将某些内容附加到数组中?

问题描述

我要制作一个自定义单元格,上面已经有一些标签,然后我创建一个单元格对象和数组,尝试将该对象附加到数组然后显示在表格上,但是在附加之后,我的数组属性中没有内容

我试图找出解决方案,但可能没有人有这些问题

//tableview实现

var recordCell : [RecordCell] = []
let note = RecordCell()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.recordCell.count
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "recordCell",for: indexPath) as! RecordCell
        let indexPath = indexPath.row


        cell.recordFileName?.text = self.recordCell[indexPath].recordFileName?.text
        cell.recordDate?.text = self.recordCell[indexPath].recordDate?.text
        cell.delegate = self
        cell.playBtn.tag = indexPath
        return cell
    }

//追加到数组

let alertController = UIAlertController(title: "請輸入錄音名稱", message: "錄音名稱", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default) { (_) in
            var name : String = ""
            if(alertController.textFields![0].text == ""){
                name = "record"
            }else{
                name = alertController.textFields![0].text!
            }

            guard self.audioRecorder == nil else{return}
                self.recordNumber += 1
                self.record.isEnabled = false
                self.pause.isEnabled = true
                self.stop.isEnabled = true
                let destinationUrl = self.getFileURL().appendingPathComponent("\(name).m4a")

                let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                                AVSampleRateKey: 44100,
                                AVNumberOfChannelsKey: 2,
                                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
                ]

                do {
                    self.audioRecorder = try AVAudioRecorder(url: destinationUrl, settings: settings)
                    self.audioRecorder.record()
                    self.note.recordFileName?.text = name
                    self.note.recordDate?.text = self.getDate()
                    self.recordCell.append(self.note)
                } catch {
                    print("Record error:", error.localizedDescription)
                }

        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (_) in
            self.dismiss(animated: true, completion: {
                self.audioRecorder.stop()
            })
        }
        alertController.addTextField { (textField) in
            textField.placeholder = "輸入名稱"
            textField.keyboardType = .default
        }
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
        self.present(alertController,animated: true,completion: nil)
    }

我希望当我附加一些东西时,数组中有一些东西

标签: arraysswifttableviewcell

解决方案


将新值附加到数组后,调用insertRows这样的方法

self.recordCell.append(self.note)
self.tableView.insertRows(at: [IndexPath(row: recordCell.count-1, section: 0)], with: .automatic)

推荐阅读