iphone - UITableView 中的点击事件

标签 iphone objective-c ios uitableview tableview

我有一个按钮和表格。现在我想以这种方式单击,每当我在 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;
}

现在,当我选择该行并按下我的按钮时,将事件放入按钮中。但没有得到正确的东西。

(IBAction)doDownload:(id)sender {
// to select first row
if(cell.tag==1)
{
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
}

最佳答案

全局声明一个int变量-

int rowNo;

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     rowNo = indexPath.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"]];
    }
}

关于iphone - UITableView 中的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594532/

相关文章:

ios - 如何确定我的 iOS 应用程序正在 DEBUG 模式下运行?

ios - 想结合使用 TouchID 和电子邮件来保护我的应用程序

iPhone - 显示无互联网连接

iphone - 调用 reloadData 后阻止更新 header ?

objective-c - NSCoder - 存档指针?

IOS SplitViewController 详细 View 的数据为空/未加载,但 View 是

ios - 如何启用/禁用来自应用程序的推送通知?

iphone - 将 NSMutableArray 保存到 Core Data

ios - 获取设置数据->常规->关于

ios - 用户拖动时导航栏设置色调颜色