我是iphone的新手。我有一个小疑问(即),我创建了一个表格视图,我将所有书名和下载选项放在每个单元格中,如下所示
Genesis Download
Exodus Download
Leviticus Download
以上是Genesis,Exodus,Leviticus是书名,下载是下载的按钮,这样的书我在桌面视图中有66本不同的书。我的问题是当我们点击下载按钮时我想得到相应的该tableview单元格的bookname。 我的代码如下所示
- (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{
}
答案 0 :(得分:3)
在按钮操作中,您可以通过访问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
}
答案 1 :(得分:1)
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);
答案 2 :(得分:0)
当您点击表格视图单元格时,将调用Delegate函数,它将为您提供索引路径部分和行值。当您在表格单元格上放置按钮时,只需将标记 - indexpath.row分配给按钮标记。在调用button函数时找到此标记,并找到aray索引以获取该数组上该索引的值。