Iphone 单击tableview单元格中的按钮时,如何检索该特定单元格标签中的文本

Iphone 单击tableview单元格中的按钮时,如何检索该特定单元格标签中的文本,iphone,Iphone,我对iphone还不熟悉。我有一个小小的疑问(即),我创建了一个表视图,我将我所有的书名和下载选项放在每个单元格中的特定书籍上,如下所示 Genesis Download Exodus Download Leviticus Download 这是上面的《创世纪》,《出埃及记》,《利未记》是书名,下载是下载这本书的按钮,我在“表视图”中有66本不同的书。我的问题是,当我们点击“下载”按钮时,我想得到相应的“表视图”单元格的书名。 我的代码如下 - (NSInteger)numbe

我对iphone还不熟悉。我有一个小小的疑问(即),我创建了一个表视图,我将我所有的书名和下载选项放在每个单元格中的特定书籍上,如下所示

Genesis    Download
Exodus     Download
Leviticus  Download
这是上面的《创世纪》,《出埃及记》,《利未记》是书名,下载是下载这本书的按钮,我在“表视图”中有66本不同的书。我的问题是,当我们点击“下载”按钮时,我想得到相应的“表视图”单元格的书名。 我的代码如下

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 66;

}



- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UIButton *downloadButton = nil;
//this is the custom cell i have created one class for this in that i am place the string titlelabel.
        CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) 
        {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
             downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
            downloadButton.frame = CGRectMake(220,10,50,30);
            [downloadButton setImage:[UIImage imageNamed:@"download.png"] forState:UIControlStateNormal];
            [downloadButton addTarget:self action:@selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            downloadButton.backgroundColor = [UIColor clearColor];
            downloadButton.userInteractionEnabled = YES;
            downloadButton.highlighted = YES;
            [cell.contentView addSubview:downloadButton];
      }    
        NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
        cell.TitleLabel.text = titleLabel;


        return cell;
    }

    -(void)downloadButtonClicked:(id)sender{

    }

在按钮操作中,您可以通过访问superView来获取单元格

-(void)downloadButtonClicked:(id)sender{
    UIButton *button = (UIButton*)sender;
    UIView *view = button.superview; //Cell contentView
    UITableViewCell *cell = (UITableViewCell *)view.superview;
    cell.textLabel.text; //Cell Text
}

当您点击表格视图单元格时,将调用委托函数,它将为您提供索引路径部分和行值。在表单元格上放置按钮时,只需将标记-indexpath.row分配给按钮标记。调用button函数并查找aray索引以获取数组中该索引的值时,请找到此标记

不客气:)@user1468157如果答案有效,别忘了接受
first set the tag of your label say it is 100.

//in the button click method...

UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];

UILabel *lbl = (UILabel *)[cell viewWithTag:100];//this is for custom label 

NSLog(@"label text =%@",lbl.text);

else  NSLog(@"label text =%@",cell.titleLabel.text);