How to insert a row in tableView without using reloadData function in Cocoa
Asked Answered
O

0

1

I been working on a self taught project in Cocoa. Trying to replicate how the media player playlist works(Play,Add and Delete and etc...).

So far, I have managed to populate a tableview from the directory using NSOpenPanel. I can able to insert a new files to the tableview and play selected sound files using AVAudioPlayer. Everything works as expected.

But When, I try to insert a new row to the tableView while the audioPlayer is playing. It updates the entire tableView and i am getting the following issues.

  • audioPlayer stopped as soon as the update performed.
  • Playing selected media file left unselected.

My Partial code below:

   var items: [URL] = [] {
   didSet {    
          tableView.beginUpdates()
          tableView.reloadData(forRowIndexes: tableView.selectedRowIndexes, columnIndexes: tableView.selectedColumnIndexes)
          tableView.reloadData()//Trying to avoid this func...
          tableView.selectRowIndexes(tableView.selectedRowIndexes, byExtendingSelection: true)
          tableView.endUpdates()
     }
   }

How do i update the tableView by adding or deleting row while the audioPlayer performing and not to reload the entire tableview.

I have done some research as below and found similar question to above issues. But it didn't helped me to fix the issue entirely.

How to update NSTableView without using -reloadData?

reloadData in NSTableView but keep current selection

Apple Doc - reloadData(forRowIndexes:columnIndexes:)

I am new to macOS development. So, help me to understand the issue and how to fix it.


Update:

Below code solves part of the issue mentioned above. Using below code keeps the selected row highlighted after update.

  var items: [URL] = [] {
  didSet {    
      let selectedRowIndexes = self.tableView.selectedRowIndexes
      tableView.reloadData()
      tableView.selectRowIndexes(selectedRowIndexes, byExtendingSelection: true)
     }
   }

But, Still audioPlayer stopped after inserting new row for some reason. my audioPlayer code as below

In ViewDidLoad: (action method)

 tableView.doubleAction = #selector(tableViewClick)

Selector code:

 func tableViewClick(_ sender: AnyObject) {
    if tableView.selectedRow < 0 { return }
    let selectedItem = items[tableView.selectedRow]
    print(selectedItem)
    do{
        try self.audioPlayer = AVAudioPlayer.init(contentsOf: selectedItem)
        audioPlayer.delegate = self
        audioPlayer.play()
    }
    catch {
        print("Error reading file attributes")
    }
}

Trying to update row/column only using reloadData(forRowIndexes:columnIndexes:). But, it not seems to be working...

  tableView.reloadData(forRowIndexes: NSIndexSet(index: tableView.selectedRowIndexes.count) as IndexSet, columnIndexes: NSIndexSet(index: tableView.selectedColumnIndexes.count) as IndexSet)  

or

  tableView.reloadData(forRowIndexes: tableView.selectedRowIndexes, columnIndexes: tableView.selectedColumnIndexes)

Update: Issue Resolved:

Issue resolved after @Leo Dabus suggestion and his accepted answer from this post BLE Peripheral disconnects when navigating to different ViewController . I created Singleton class as below

class Shared { 
    private init(){ } 
    static let instance = Shared()
    var audioPlayer = AVAudioPlayer()
}

And access the same instance as below.

Shared.instance.audioPlayer = audioPlayer

Hope this helps someone....

Oakes answered 26/11, 2017 at 10:47 Comment(7)
Why is the AVAudioPlayer stopping? is it owned by a tableViewCell? if so maybe move it to the view controller or to an AudioPlayerPool class and delegate back the respondsibility for starting the player.Amongst
thanks for the suggestion. i will try and I dont have any seperate class for AudioPlayer. but i do have a tableview action method in viewDidLoad tableView.action = #selector(tableViewClick)Oakes
New rows are automatically updated. Just insert or remove the row.Nambypamby
@Nambypamby Thanks. I have no problem inserting or removing rows.problem is why the audioPlayer stop running after row insert. check my update and let me know on comment.Oakes
Why do you do reloadData?Nambypamby
@Nambypamby Thanks. I am bit confused. how can i add a new row without reload func. i tried without reload func but nothing get called...Oakes
Insert a row in the table view with insertRows(at:withAnimation:).Nambypamby

© 2022 - 2024 — McMap. All rights reserved.