I have a button and Table. Now I want to click in such way that Whenever I select any row in tableview
and press the button, that particular button press event would happen. For that, firstly I have give tag to each row i.e
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *LabelCellIdentifier = @"cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:LabelCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LabelCellIdentifier];
}
if (indexPath.row <= [_arrayMp3Link count]) {
cell.textLabel.text = [_arrayMp3Link objectAtIndex:indexPath.row];
}
// now tag each row
NSInteger count = 1;
for (NSInteger i = 0; i < indexPath.section; i++) {
count += [[tableView dataSource] tableView:tableView numberOfRowsInSection:i];
}
count += indexPath.row;
// dequeue, create and configure...
cell.tag = count;
return cell;
}
and now in putting event in button when I select the row and press my button. But not getting the correct things.
(IBAction)doDownload:(id)sender {
// to select first row
if(cell.tag==1)
{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];
}
Declare an int
variable globally -
int rowNo;
Then assign value to it in didSelectRowAtIndexPath:
method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
rowNo = indexPath.row;
}
Now you have the indexNo. of selected row.
-(IBAction)doDownload:(id)sender
{
//Now compare this variable with 0 because first row index is 0.
if(rowNo == 0)
{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];
}
}
You have to use the function didSelectRowAtIndexPath method of tableView.
in that method you save the selected row tag or whatever you want to save.and in the button action check the value of the saved entity and do anything.
Use the datasource function of UITableView which is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
which indexPath.row is each row's index.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==i)
{//Perform whatever action you would like for whichever row number..i'm just using i as an int placeholder
[[UIApplication sharedApplication]openURL:[NSURLWithString:@"http://www.google.com"]];
}
//Or you could perform the same action for all rows in a section
if(indexPath.section==i)
{
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:@"http://www.google.com"]];
}
'the-interview-2.jpg' is name of a image file
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewc"];
[self.navigationController pushViewController:vc animated:YES];
[vc setImageName:@"the-interview-2.jpg"];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With