Christien
Christien

Reputation: 1213

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

Upvotes: 23

Views: 43447

Answers (7)

cronos-x
cronos-x

Reputation: 1

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
    }
}

Upvotes: 0

Madan gupta
Madan gupta

Reputation: 668

'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"]; 
}

Upvotes: 1

TheTiger
TheTiger

Reputation: 13354

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

Upvotes: 31

NANNAV
NANNAV

Reputation: 4901

//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"]]; 
        }

Upvotes: 0

KarenAnne
KarenAnne

Reputation: 2814

Use the datasource function of UITableView which is

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

which indexPath.row is each row's index.

Upvotes: 2

Tommy Devoy
Tommy Devoy

Reputation: 13549

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

}

Upvotes: 2

tausun
tausun

Reputation: 2162

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.

Upvotes: 4

Related Questions