infinitered / redpotion

We believe iPhone development should be clean, scalable, and fast with a language that developers not only enjoy, but actively choose. With the advent of Ruby for iPhone development the RubyMotion community has combined and tested the most active and powerful gems into a single package called RedPotion
MIT License
233 stars 40 forks source link

Using DataTableScreen - Tried to delete row with on_cell_deleted but couldn't #163

Closed jr180180 closed 8 years ago

jr180180 commented 8 years ago

Hey RedPotion team! I'm not sure if this is an issue. Could be an opportunity to enhance RedPotion.

Going through the getting started tutorial, I created a DataTableScreen with a CDQ Model. When I tried deleting a row, it wouldn't allow me to do so using the on_cell_deleted method. I couldn't access the CDQ methods from within that method.

I posted about this in the RubyMotion forum.

Within my controller, I had to use delete_row instead:

def delete_row(index_path)
    @names.all[index_path.row].destroy
    cdq.save #  you need to call this for persistance purposes
end

If that's actually how it's suppose to work or if you have any advice on this, please let me know. :+1:

Thanks a ton!

markrickert commented 8 years ago

Yes. That's how it's supposed to work. Basically you need to delete the object from the database with the on_cell_deleted method but return false so the table view doesn't try and delete the cell, the database sends a notification that the table listens to when it's delete. Returning false from that method basically tells the table that it's not allowed to delete the row.

jr180180 commented 8 years ago

Ahhh, gotcha. I think I understand now.

You're saying I could have written it like:

  def on_cell_deleted(cell)
    @names.all[cell[:arguments][:id]-1].destroy
    cdq.save

    false
  end

and it would skip this part, right?

        unless delete_cell == false
          self.promotion_table_data.delete_cell(index_path: index_path)
          deletable_index_paths << index_path
        end

Looks like when I tried that the first time around, I forgot to return false. Stupid me. :(

markrickert commented 8 years ago

You got it. That should work for deleting rows.

jr180180 commented 8 years ago

Thanks for your help here, @markrickert ! Really appreciate it.

I love how this works so freakin' well!

For others reading this, I originally created an id argument in my model

 def cell
    {
      # Use the model's properties to populate data in the hash
      title: name,
      subtitle: "Something else: #{something_else} - #{id}",
      action: :msg,
      editing_style: :delete,
      arguments: {id: id}
    }
  end

and used that for the on_cell_deleted above.

Hence:

@names.all[cell[:arguments][:id]-1].destroy

Turns out, the id argument isn't even necessary. You can pass a second parameter index_path into your on_cell_deleted method and easily remove the row with that. So much easier then trying to make sure you're creating unique IDs for your rows. :)

Here's what you can use instead:

  def on_cell_deleted(cell, index_path)
    @names.all[index_path.row].destroy
    cdq.save

    false
  end

_High Fives to gem authors_ :hand:

Hope this thread helps someone!