2014-05-24 74 views
-1

我有多個單元格都帶有按鈕。創建SimpleTableCell.xib與按鈕refrencing出口navSimpleTableCell.hSimpleTableCell.m。然後將這些細胞被稱爲ImagesTableViewController.mImagesTableViewController.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]; 
        }]; 

} 

我想在indexPath.row上的單元格按鈕時,點擊相關聯。這樣我就可以識別哪個單元被按下了。

我該怎麼做?

回答

1

你可以改變你的處理程序接受觸發事件的UIButton

-(void)naviguate:(id)sender 

記住更改addTargetselector告訴它期望參數:

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

然後,你可以繼承你的UIButton並將索引路徑添加爲您在創建單元格時設置的屬性。

在處理程序中,子類型UIButton作爲發件人傳遞,您可以訪問索引路徑屬性值。

+0

你如何繼承您的按鈕,並添加索引路徑爲您設置屬性當單元格被創建時。 – maxisme

+0

無法編輯我的答案,所以一定要在這裏做。創建一個新的類,它的子類UIButton並添加索引路徑屬性。在你的SimpleTableCell.xib中選擇你的UIButton並在屬性檢查器中將類從UIButton更改爲你的新類。 – Calvedos

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]; 
       }]; 

}