单击UITableView中的Event

Chr*_*ien 21 iphone objective-c uitableview tableview ios

我有一个按钮和表.现在我想以这样的方式点击:每当我选择任何一行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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

The*_*ger 30

int全局声明变量 -

int rowNo;
Run Code Online (Sandbox Code Playgroud)

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     rowNo = indexPath.row;
 }
Run Code Online (Sandbox Code Playgroud)

现在你有了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"]];
    }
}
Run Code Online (Sandbox Code Playgroud)


tau*_*sun 5

你必须使用tableView的函数didSelectRowAtIndexPath方法.

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