如何正确地添加一些东西到数组?

时间:2019-05-29 02:16:24

标签: arrays swift tableview cell

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

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

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

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

2 个答案:

答案 0 :(得分:0)

在向数组添加新值之后,像这样调用insertRows方法

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

答案 1 :(得分:0)

我没发现任何问题。

确定要调用您的代码吗?尝试在添加之前立即给print(),看看它是否被调用。

也许由于guard self.audioRecorder == nil else{return}

而未被调用