识别表格中的单元格

时间:2014-05-24 21:49:55

标签: ios objective-c uitableview

我有多个单元格都带有一个按钮。在SimpleTableCell.xib中创建了nav SimpleTableCell.h的“{1}}”按钮。然后在SimpleTableCell.mImagesTableViewController.m中调用这些单元格,如下所示:

ImagesTableViewController.h

其中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableCell"; SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } NSString *stringy = @"http://www.example.co.uk/"; NSString *link = [stringy stringByAppendingString: images[indexPath.row]]; NSString* encodedString = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; SDImageCache *imageCache = [SDImageCache sharedImageCache]; [imageCache queryDiskCacheForKey:encodedString done:^(UIImage *image, SDImageCacheType cacheType) { if (image){ cell.thumbnailImageView.image = image; }else{ [cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { [imageCache storeImage:image forKey:encodedString]; }]; } }]; [cell.nav addTarget:self action:@selector(naviguate) forControlEvents:UIControlEventTouchUpInside]; } 是:

naviguate

我希望在点击单元格按钮时关联-(void)naviguate{ [UIView animateWithDuration:0.5 delay:0 options: UIViewAnimationOptionCurveEaseOut animations:^{ [_tableView setFrame:CGRectMake(0, 569, _tableView.frame.size.width, _tableView.frame.size.height)]; } completion:^(BOOL finished){ [self performSegueWithIdentifier:@"link" sender:self]; }]; } 。这样我就可以确定哪个细胞被按下了。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您可以更改处理程序以接受触发事件的UIButton

-(void)naviguate:(id)sender

请记住更改addTarget selector以告知其预期参数:

[cell.nav addTarget:self 
             action:@selector(naviguate:) 
   forControlEvents:UIControlEventTouchUpInside];

然后,您可以将UIButton子类化,并将索引路径添加为您在创建单元格时设置的属性。

在处理程序中,子类UIButton作为发件人传递,您可以访问索引路径属性值。

答案 1 :(得分:0)

您可以使用按钮上的tag属性来存储行。在cellForRowAtIndexPath -

[cell.nav addTarget:self action:@selector(naviguate:) forControlEvents:UIControlEventTouchUpInside];
cell.nav.tag=indexPath.row;

然后在您的事件处理程序中,您会收到发送按钮,您只需检索标记

即可
-(void)naviguate:(id)sender {

  UIButton *theButton=(UIButton *)sender;

  // You can now get the row by accessing theButton.tag

  [UIView animateWithDuration:0.5
                      delay:0
                    options: UIViewAnimationOptionCurveEaseOut
                 animations:^{

                     [_tableView setFrame:CGRectMake(0, 569, _tableView.frame.size.width, _tableView.frame.size.height)];
                 }
                 completion:^(BOOL finished){
                     [self performSegueWithIdentifier:@"link" sender:self];
                 }];

}