简体   繁体   中英

Get data from UITableView cell

I have a problem with getting data from cells in UITableView.
I have a lots of cells (other class with xib) with some people info in table view. Every cell has own switch.

Under table is button that will send message to every person with switch pressed on.
I set property in people class that will remember if switch for person is on. Every switch in cellForRow method has set selector with method for switch action.

Problem is that I have no idea, where to set person property value about switch state. In selector method I don't have access to person info.
I have sender, but no info about person.

I'd do this in one of 2 ways depending on what exactly you're trying to do:

  1. Pass a pointer of the person object to the cell when you're configuring it and let the cell handle updating the property that holds the value of the switch.
  2. Make a protocol with a function

     -(void)switchInCell:(UITableViewCell*) cell valueModifiedTo:(NSInteget) newValue 

    Adopt the new protocol in your view controller, add a delegate property of type new protocol to the cell, call that delegate method from within the cell if the switch value changes.

    In that method implementation in your view controller, you can do

     NSIndexPath* indexPath = [theTable indexPathForCell:cell]; PersonClass* person = Your_DataSource_Array [indexPath.row]; //modify person properties 

You can set the UIView tag property to a number, for each of the subviews of the table View Cell's "content" property, at the cell's creation time (eg when you add all the UIViews, eg labels, buttons, etc...), giving each subview a unique value.

Later, if just given the UITableViewCell, you can iterate through the subviews of is content view and locate your switch (and any other UIViews) by checking their tag value.

If you are using custom cells, you can have method for your switch inside the cell's implementation. From the method, you can get back to the data source using delegation, or notifications and you can pass indexPath of the cell for further identification of the object in the data. You can have a property to store the indexPath of the cell in implementation of the cell's class and you can set the value to that property in cellForItemAtIndexPath: method. Good Luck!

you said you have sender. then you need to just make custom subclass of the control you are using and create a custom property like this.

@property (nonatomic, strong)NSIndexPath *indexPath;

and set it in cellforrowatindexpath. when your selecter calls the you can parse the sender to the custom created class and get the value form the property. try this once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM