23

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"]]; 
}
UsamaMan
  • 695
  • 10
  • 28
Christien
  • 1,213
  • 4
  • 18
  • 32

7 Answers7

31

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"]];
    }
}
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • ok thanks! and if want same action with every row..do i need to use index path.section?? and what in do download? – Christien Sep 26 '12 at 05:25
  • And you want to call only [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; on button click after select any row ? – TheTiger Sep 26 '12 at 05:31
  • if you want to do so for every row then think simply you can do it just -(IBAction)doDownload:(id)sender method ... because no matter which row is clicked you just have to do same thing on button click.... there is no need to any comparision – TheTiger Sep 26 '12 at 05:35
4

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.

tausun
  • 2,154
  • 2
  • 24
  • 36
  • k but to select a row and to press button which thing i need to be used – Christien Sep 26 '12 at 04:39
  • if you want the action when you select the table row,which is better,then you can check the tag of cell inside the didiSelectRowAtIndexPath func and do what u want.But if u must use the selection and button press both then use both. – tausun Sep 26 '12 at 04:47
2
-(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"]]; 

}
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
  • ya thanx man! but i want whenever I select a row and click button which i have given separately. – Christien Sep 26 '12 at 04:45
  • Sorry, I don't understand. Are you saying you want to be able to do the same thing if you click the button and the row? Or what are you trying to do? – Tommy Devoy Sep 26 '12 at 04:48
  • thanx.I got it ..please check whether i have used right code to tag every row. – Christien Sep 26 '12 at 04:53
2

Use the datasource function of UITableView which is

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

which indexPath.row is each row's index.

KarenAnne
  • 2,764
  • 1
  • 28
  • 21
1

'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"]; 
}
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Madan gupta
  • 668
  • 6
  • 19
0
//use selectedrow to check the condition

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

                     selectedrow=indexPath.row;
    }

        -(IBAction)doDownload:(id)sender {
        // to select first row
        if(selectedrow==1)
        {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
        }
NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

If you want to push views from the side-menu. The top part is the design the switch case statement works well, the contact shows a view on top and the others open navigation controllers first.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = items[indexPath.row]
    cell.textLabel?.textColor = .white
    cell.backgroundColor = .systemTeal
    cell.imageView?.tintColor = .white

    switch indexPath.row {
    case 0:
        cell.imageView?.image = UIImage(systemName: "info.circle")
        //presentViewController(AboutViewController, animated: true, completion: nil)
        //performSegue(withIdentifier: "toLanguageView", sender: nil)
    case 1:
        cell.imageView?.image = UIImage(systemName: "map")
    case 2:
        cell.imageView?.image = UIImage(systemName: "figure.walk")
    case 3:
        cell.imageView?.image = UIImage(systemName: "paperplane")
    default:
        break
    }

    return cell
    }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    
    switch indexPath.row {
    case 0:
                    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
        self.navigationController?.pushViewController(controller, animated: true)
                
    case 1:
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "DigMenuViewController") as! DigMenuViewController
        self.navigationController?.pushViewController(controller, animated: true)
        
    case 2:
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "VirtualTourViewController") as! VirtualTourViewController
        self.navigationController?.pushViewController(controller, animated: true)
        
    case 3:
        
        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "ContactViewController") as! ContactViewController
        self.present(newViewController, animated: true, completion: nil)
        
    default:
        break
    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
cronos-x
  • 1
  • 1