How can I program buttons in custom cells according to the selected cell accordingly? ios

StackOverflow https://stackoverflow.com/questions/22209997

  •  09-06-2023
  •  | 
  •  

質問

So I have a table in notificationsViewController with customs cells. this tableView loads data from jsonSerialization to display data, specifically customerID and checkinID

the custom cell is named checkInCell.m, .xib, and .h. In this custom cell, there are two buttons, one for accepting a check-in and rejecting a check in with API calls. (sample url: api/CheckIn/checkInConfirmation?CustomerID=%@&MerchantID=%@&confirm=%@&CheckInID=%@")

my question is how do i pass the customerID and checkinID from the notificationsViewController for the selected cell to the custom cell buttons, so I can perform accepting and rejecting check-ins?

also, how can i reload the data of the table, because I don't think the code below would work the same if I wanted to reload the data from the checkinCell.

[self.notificationsTableview reloadData];

i tried doing notificationViewcontroller.notificationsTableView reload data, but I think I'm missing something. Thanks for the help!

役に立ちましたか?

解決

In your tableView:cellForRowAtIndexPath: override, after you have accessed your cell with either dequeueCell... or by loading the nib file, you'll need to configure the retrieved cell with the customerID and checkinID and whatever other information is displayed. Typically your tableView:cellForRowAtIndexPath looks something like:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCell* cell = [tableView dequeueCellWithReusableIdentifier:@"MyCell"];
    if(!cell)
    {
        cell = .... some other mechanism of loading cell from your nib file
    }

    // configure cell here
    cell.customerID = ...
    cell.checkinID = ...

    return cell;
}

Note that the "load from nib file" part is greatly simplified if you use a storyboard as you can use dequeueCellWithReusableIdentifier:forIndexPath: and it will "always" return a cell for you.

The buttons should have their target and action set to the cell itself in your nib file. Now when the buttons are pressed, a method on the custom cell will be executed and you have access to the data passed in as part of your configuration above.

I'm not sure what your question is about reloadData. reloadData will cause all all data in the tableview (but not your controller) to be reloaded from the delegate and dataSource (presumably your controller) If you wish to force just a single section (or row) to be reloaded, there are also reloadSections... and reloadRows... variants.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top