Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking Event in UITableView

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"]]; 
}
like image 294
Christien Avatar asked Sep 26 '12 04:09

Christien


5 Answers

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"]];
    }
}
like image 143
TheTiger Avatar answered Nov 08 '22 08:11

TheTiger


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.

like image 23
tausun Avatar answered Nov 08 '22 08:11

tausun


Use the datasource function of UITableView which is

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}

which indexPath.row is each row's index.

like image 39
KarenAnne Avatar answered Nov 08 '22 10:11

KarenAnne


-(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"]]; 

}
like image 1
Tommy Devoy Avatar answered Nov 08 '22 10:11

Tommy Devoy


'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"]; 
}
like image 1
Madan gupta Avatar answered Nov 08 '22 10:11

Madan gupta