Search code examples
iosrubyuitableviewrubymotion

IOS: How to convert NSArray to NSIndexPath


I want to do a batch insertion into UITableview.

MyArray.all() do |datas|
  self.tableView.beginUpdates
  self.tableView.insertRowsAtIndexPaths(datas, withRowAnimation: UITableViewRowAnimationFade)
  self.tableView.endUpdates
  self.myarrayvar += datas
end

'myarrayvar' is an array of MyArray. I tried to refer to apple documentation

and stackoverflow which is similar to my question.

But I really no idea how to convert my datas, to fit into the parameter of insertRowsAtIndexPaths function.

Using rubymotion


Solution

  • Here's one way that I'm currently using:

    def insertCellsAtBottom(newData)
      # Get count for the existing data you have.
      currentCount = @data.count
    
      # Build the indexPaths for the new data, adding to the current count each time.
      # This will make sure you are creating the correct indexes.
      indexPaths = newData.map.with_index do |obj, index|
        NSIndexPath.indexPathForRow(currentCount + index, inSection: 0)
      end
    
      # Update your datasource and insert the rows.
      self.tableView.beginUpdates
      @data.addObjectsFromArray(newData)
      self.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimationFade)
      self.tableView.endUpdates
    end