我正在创建一个音频录音机,每次上传后,该名称都会在表视图(请注意:带有UITableView与.NET的常规视图控制器)中将int附加到录音(例如1、2、3等)上。表格视图控制器)。
我在删除每一行时遇到问题,并且不确定是否是因为'numberOfRecords.remove(at:indexPath.row)'仅接受字符串。
我收到错误消息:“类型'Int'的值没有成员'remove'。”
class ViewController2: UIViewController, RecordButtonDelegate, AVAudioRecorderDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
var numberOfRecords : Int = 0
// Setting up Table View
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfRecords
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = String(indexPath.row + 1)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let path = getDirectory().appendingPathComponent("\(indexPath.row + 1).m4a")
do {
audioPlayer = try AVAudioPlayer(contentsOf: path)
audioPlayer.play()
}
catch {
}
}
// Delete rows
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
numberOfRecords.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
// Audio Player
var audioPlayer : AVAudioPlayer!
var recordingSession : AVAudioSession!
var audioRecorder : AVAudioRecorder!
var recordButton: RecordButton?
@IBOutlet weak var buttonLabel2: RecordButton!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
buttonLabel2.delegate = self
}
func tapButton(isRecording: Bool) {
// Check if we have an active recorder
if audioRecorder == nil {
numberOfRecords += 1
let filename = getDirectory().appendingPathComponent("\(numberOfRecords).m4a")
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]
// Start audio recording
do {
audioRecorder = try AVAudioRecorder(url: filename, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
}
catch {
displayAlert(title: "Oops!", message: "Recording failed")
}
// Play speaker instead of earpiece
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch let error as NSError {
print("Audio Session error: \(error.localizedDescription)")
}
}
else {
// Stop audio recording
audioRecorder.stop()
audioRecorder = nil
UserDefaults.standard.set(numberOfRecords, forKey: "myNumber")
myTableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Setting up Recording session
recordingSession = AVAudioSession.sharedInstance()
if let number : Int = UserDefaults.standard.object(forKey: "myNumber") as? Int {
numberOfRecords = number
}
AVAudioSession.sharedInstance().requestRecordPermission { (hasPermission) in
if hasPermission {
print ("Accepted")
}
}
答案 0 :(得分:0)
您遇到的问题是由于您在Int上调用remove(at:)
。一个Int没有名为remove(at:)
的函数。
您要声明var numberOfRecords: Int
来跟踪索引,然后在
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
您正在打电话
// this is the line that's causing your problem
numberOfRecords.remove(at: indexPath.row)
如果您想继续使用整数来跟踪单元格,则应从numberOfRecords
减去
numberOfRecords-=1
或者您可以使用类似以下的数组来跟踪记录:
// declare an array of Strings to hold your filenames
var records: [String] = []
然后在保存文件的位置,将新文件名添加到表视图的数组中
// Stop audio recording
audioRecorder.stop()
audioRecorder = nil
// add your filename to your array of records for the tableview
records.append(filename)
// update your total number of records if desired
UserDefaults.standard.set(numberOfRecords, forKey: "myNumber")
myTableView.reloadData()
然后您的委托函数可能看起来像这样
// Setting up Table View
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return the total count of filenames in your array
return records.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// set the filename in your text label
cell.textLabel?.text = records[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// get the filename for this row
let filename = records[indexPath.row]
// use your filename in your path
let path = getDirectory().appendingPathComponent("\(filename)")
do {
audioPlayer = try AVAudioPlayer(contentsOf: path)
audioPlayer.play()
}
catch {
}
}
,您可以更新remove(at:)
调用以改为在记录数组上运行
records.remove(at: indexPath.row)
编辑:
当您从记录数组中添加或删除文件名时,将使用更新的记录来更新用户默认值:
// save array to user defaults when you create a new record or when you delete a record
UserDefaults.standard.setValue(records, forKey: "storedRecords")
要检索保存的文件名数组,请从用户默认值中将其拉出,并使用存储的名称更新记录数组。
将这些行替换为viewDidLoad
函数:
if let number : Int = UserDefaults.standard.object(forKey: "myNumber") as? Int {
numberOfRecords = number
}
与此:
// load stored records from user defaults and verify it's what you expect to receive
if let stored = UserDefaults.standard.value(forKey: "storedRecords") as? [String] {
// update your records array with the stored values
records = stored
}