Reputation: 1878
I have a json array with certain objects : first name , last name, unique id. I am able to parse them and save them in dictionary and also check them for NULL conditions.
I have created a table in which i am able to display the names. Also, i have one button in each row as well and i have tagged them. What i want is that when i click the button, i am able to see the unique id fetched from server in my console. I need to pass this unique id to next view as using this unique id would help me to display view corresponding to that id on server.
Click here to see what I have done so far
I have made some changes by adding this to my viewDidLoad method:
for(NSDictionary *items in infos)
{
....
....
for(int i =0; i < len; i++)
{
...
...
...
for(NSDictionary *newItems in infoArray)
{
...
...
NSString *folks_IdRcv = [[items objectForKey:strr]objectForKey:@"folks_id"];
NSLog(@"FolksID = %@", folks_IdRcv);
NSString *folksIDString = [NSString stringWithFormat:@"%@", folks_IdRcv, nil];
NSLog(@"folkid_display=%@", folksIDString);
if((fName && lName && email_Id && number_id) && displayName && folksIDString != NULL)
{
...
...
[folksIDArray insertObject:folksIDString atIndex:ii];
NSLog(@"FolksID String Name = %@", [folksIDArray objectAtIndex:(ii)]);
ii++;
}
}
}
}
NSMutableArray *nArray = [[NSMutableArray alloc]initWithCapacity:len];
for(int j =0; j<len ;j++)
{
...
...
[nArray addObject:[folksIDArray objectAtIndex:j]];
NSLog(@"FID Element=%@", nArray);
}
self.infos = mArray;
[super viewDidLoad];
}
And for this particular code, i am getting the following response:
FID Element=(
400386528950544,
162622322666106,
643171434889706
)
Now, I have a table view in which i have one button for each row and i am able to display the first and last names in each row. What I want now is that whenever I click the button, the unique id for that particular name should be saved in the tag so that i can send that id to the server on button click action.
Please help me out.
Upvotes: 3
Views: 859
Reputation: 7461
Here, I assume that you can make array of unique id array and this array should be declare in .h file or should be class member. Than use following code.
-(IBAction)buttonClicked1:(id *)sender{
NSString *UIDString = [uidarray objectAtIndex:[sender tag]]; // uidarray is unique id array
NSLog(@"uid for particular button=%@", UIDString);// this will give you kid for button that you tapped or clicked.
}
Upvotes: 2
Reputation: 2285
Tag is one way another way is sender superview. Refer below code (Not require to set tag)
- (IBAction)clickedButton:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
NSString* id = [Array objectAtIndex:indexPath.row];
}
and if you are using tag then make sure tag must be unique.
Upvotes: 1
Reputation: 20541
I have one Idea for your requirement... In TableView Delegate Method.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.............your another code........
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, 14, 14);
button.frame = frame; // match the button's size with the image size
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}
after paste this method ...
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:Yourtableview];
NSIndexPath *indexPath = [Yourtableview indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: Yourtableview accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
and also in accessoryButtonTappedForRowWithIndexPath Delegate method of tableview....
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [self.arrTypes objectAtIndex:indexPath.row];//sel.arrTypes is Array Data..use your Data here and
....... do somthing here which you want ......
}
Upvotes: 1
Reputation: 8947
use this UIButton* btn
.....
btn.tag.
and when button pressed
UIButton *button = (UIButton*)sender;
thetatosendserver = button.tag
Upvotes: 0