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