我有多個單元格都帶有按鈕。創建SimpleTableCell.xib
與按鈕refrencing出口nav
SimpleTableCell.h
SimpleTableCell.m
。然後將這些細胞被稱爲ImagesTableViewController.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];
}];
}
我想在indexPath.row
上的單元格按鈕時,點擊相關聯。這樣我就可以識別哪個單元被按下了。
我該怎麼做?
你如何繼承您的按鈕,並添加索引路徑爲您設置屬性當單元格被創建時。 – maxisme
無法編輯我的答案,所以一定要在這裏做。創建一個新的類,它的子類UIButton並添加索引路徑屬性。在你的SimpleTableCell.xib中選擇你的UIButton並在屬性檢查器中將類從UIButton更改爲你的新類。 – Calvedos