当我点击放在单元格中的按钮时,我想得到单元格中显示内容的一些数据。为此,我实现了使用indexPathForSelectedRow方法。但是当我点击单元格时,该方法返回0索引,即使我没有点击第0个索引单元格。这是我用于按钮的代码 -
- (IBAction)dataSaveLounge:(id)sender {
NSIndexPath *path = [self.loungeTable indexPathForSelectedRow];
NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];
NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];
NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];
saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];
}
编辑:添加了cellfForRowAtIndex方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *loungeCellID = @"MyCellLounge";
LoungeTableCell *loungeCell = [tableView dequeueReusableCellWithIdentifier:loungeCellID];
if (loungeCell == nil) {
loungeCell = [[LoungeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCellLounge"];
}
loungeCell.loungeTitle.text = [[loungeData objectAtIndex:indexPath.row]valueForKey:@"title"];
loungeCell.loungeSubtitle.text = [[loungeData objectAtIndex:indexPath.row]valueForKey:@"excerpt"];
dispatch_async(dispatch_get_main_queue(), ^{
loungeCell.loungeImgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"default"]];
[loungeCell.loungeImgView sd_setImageWithURL:[NSURL URLWithString:[[loungeData objectAtIndex:indexPath.row] valueForKey:@"banner_image"]]];
});
return loungeCell;
}
我在按钮点击时将一些数据保存在核心数据中,因此最后一个方法调用
答案 0 :(得分:0)
如果您单击行中的按钮,请尝试此操作 设置按钮标记
cell.button.tag = indexPath.row
[cell.button addTarget:self action:@selector(dataSaveLounge:) forControlEvents:UIControlEventTouchUpInside];
在 cellForRowAtIndexPath
中- (IBAction)dataSaveLounge:(id)sender {
UIButton *pressedButton = (UIButton *)sender;
NSLog(@"Tag of button pressed:%d",pressedButton.tag);
}
答案 1 :(得分:0)
在cellForRowAtIndexPath
例如loungeCell.btn = indexPath.row;// set your button IBOutlet name here
然后访问
- (IBAction)dataSaveLounge:(id)sender {
UIButton *btn = (UIButton *)sender;
NSString *titleSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"title"];
NSString *subtitleSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"excerpt"];
NSString *idSave = [[loungeData objectAtIndex:btn.tag] valueForKey:@"id"];
saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];
}
答案 2 :(得分:0)
尝试以下代码:
用以下方法替换dataSaveLounge
方法:
- (IBAction)dataSaveLounge:(id)sender {
CGPoint center= sender.center;
CGPoint viewPoint = [sender.superview convertPoint:center toView:self.loungeTable];
NSIndexPath *path = [self.loungeTable indexPathForRowAtPoint:viewPoint];
NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];
NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];
NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];
saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];
}
OR
- (IBAction)dataSaveLounge:(id)sender {
CGPoint btnPosition = [sender convertPoint:CGPointZero
toView:self.tableView];
NSIndexPath *path = [self.loungeTable indexPathForRowAtPoint:btnPosition];
NSString *titleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"title"];
NSString *subtitleSave = [[loungeData objectAtIndex:path.row] valueForKey:@"excerpt"];
NSString *idSave = [[loungeData objectAtIndex:path.row] valueForKey:@"id"];
saveMdl = [FavouritesModel saveFavourites:titleSave and:subtitleSave and:idSave];
}
答案 3 :(得分:0)
在UITableView中为此目的使用标记是有风险的。如果添加或删除任何行,标记将不会在现有可见行上更改,并且您将指向错误的索引。
请参阅此答案以获得更好的解决方案https://stackoverflow.com/a/43036216/1189470
答案 4 :(得分:0)
您也可以这样获得NSIndexPath
。如果您有多个部分,此代码也可以帮助您。并确保LoungeTableCell
- (IBAction)dataSaveLounge:(id)sender {
LoungeTableCell *cell = (LoungeTableCell *) sender.superview.superview;
NSIndexPath *path = [self.loungeTable indexPathForCell:cell];
}