ios UITableView 中的点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12594532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:23:13  来源:igfitidea点击:

Clicking Event in UITableView

iphoneobjective-ciosuitableviewtableview

提问by Christien

I have a button and Table. Now I want to click in such way that Whenever I select any row in tableviewand press the button, that particular button press event would happen. For that, firstly I have give tag to each row i.e

我有一个按钮和表。现在我想以这样的方式点击,每当我选择任何一行tableview并按下按钮时,特定的按钮按下事件就会发生。为此,首先我给每一行打上标签,即

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

回答by TheTiger

Declare an intvariable globally -

int全局声明一个变量 -

int rowNo;

Then assign value to it in didSelectRowAtIndexPath:method

然后在didSelectRowAtIndexPath:方法中为其赋值

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     rowNo = indexPath.row;
 }

Now you have the indexNo. of selected row.

现在你有了 indexNo。所选行的。

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

回答by tausun

You have to use the function didSelectRowAtIndexPath method of tableView.

您必须使用 tableView 的函数 didSelectRowAtIndexPath 方法。

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.

在该方法中,您保存选定的行标记或任何您想保存的内容。在按钮操作中检查保存的实体的值并执行任何操作。

回答by KarenAnne

Use the datasource function of UITableView which is

使用 UITableView 的数据源函数

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

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

which indexPath.row is each row's index.

其中 indexPath.row 是每一行的索引。

回答by Tommy Devoy

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

}

回答by Madan gupta

'the-interview-2.jpg' is name of a image file

'the-interview-2.jpg' 是图像文件的名称

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

回答by NANNAV

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