Peter
Peter

Reputation: 21

Values from textfields in UITableViewCell - iOS ObjC

I have a small problem with cells in uitableview.

I create properties in one cell and i want to grab values from them and sent to my webservice:

@property (weak, nonatomic) IBOutlet UILabel *profileValueLabel;
@property (weak, nonatomic) IBOutlet UITextField *profileTextfield;

profileValueLabel is just text, it is ok but I need to take profileTextField value and send it with my service out.

Any help?

Upvotes: 1

Views: 297

Answers (3)

user2320861
user2320861

Reputation: 1431

Your profileTextfield property needs to be in the header file for your UITableViewCell subclass. Then in your UITableViewDataSource controller you would use [myTableView cellForRowAtIndexPath:yourCellsIndexPath].profileTextfield.text once you've determined the index path for the cell whose textfield text you want to grab.

If you want to grab the profileTextfield property as soon as your user is done entering data, you will need to create a protocol in your UITableViewCell subclass and a corresponding delegate property. Then you make a protocol method like -(void)tableViewCell:(myUITableViewCellSubclass *)cell didEndEditingProfileTextFieldWithText:(NSString *)text; Then in your UITableViewCell subclass, set the profileTextField's delegate to your UITableViewCell subclass and use the textFieldDidEndEditing: or textFieldWillReturn: delegate method. In whichever method you use, call [self.delegate tableViewCell:self didEndEditingProfileTextFieldWithText:self.profileTextfield.text] protocol method. Your tableViewController should handle this method, and you can do whatever you need to there.

Upvotes: 0

Saket Kumar
Saket Kumar

Reputation: 1197

I hope you have implemented the delegates of UITableView , onE Approach NSString *tempString1 ; NSString *tempString2 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
tempString1= cell1.textLabel.text; //<-- your label 

*tempString2= cell1.textfeild.text ; // u can access the value , 

} second part of question //
You need to embed tempString and tempString2 variable with the service request .

Upvotes: 0

Coach
Coach

Reputation: 339

Your best bet would be to have your UITableViewCell subclass conform to the UITextFieldDelegate, and then create a protocol (delegate) for your table view cell subclass that fires when the UITextField didEndEditing method is called and pass the table view cell itself (perhaps along with the text data you want to send to your web service) into the delegate method so that in your view controller you can determine the index path based on the cell in the table. Then, in your view controller, you can send the data on each cell to the web service whenever you delegate method is fired. This is probably the cleanest approach, but it's best to encapsulate this logic in the table view class itself so as to keep your view controller thinner and make the communication between the two components more explicit.

Upvotes: 1

Related Questions