[英]How to determine a Button click in a Custom UITableViewCell?
我有一个带有自定义单元格和节标题的TableView。 标头向我显示国家,自定义单元格向我显示城市。
看起来像这样:
美国
纽约
洛杉矶
德国
柏林
法兰克福
每个单元都有一个按钮。 按下后,它将导航并将城市名称推送到详细信息视图。
问题是,按下按钮后,我不知道如何确定城市名称。
我有一个解决方案,但是它不再起作用了:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
StandortCell *cell = (StandortCell *)[tableView dequeueReusableCellWithIdentifier:@"ortCell"];
[cell.detailButton setTag:indexPath.row];
cell.ortLabel.text = [managedObject valueForKey:@"stadtname"];
}
- (IBAction)detailButton:(id)sender {
UIView *contentView = [sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
NSInteger section = cellIndexPath.section;
UIButton * myButton = sender;
int row=myButton.tag;
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section];
StrasseViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"strasse"];
self.selectedStandort = [self.fetchedResultsController objectAtIndexPath:indexPath];
detail.currentStandort = self.selectedStandort;
[self.navigationController pushViewController:detail animated:YES];
}
当我使用旧代码时,它只会在第一部分中显示正确的街道行。 它没有检测到其他部分(国家);
当我选择第三部分的第一个城市时。 它会在第一部分向我显示第一个城市。
我希望你能帮助我。
使用它,希望它能解决您的目的。
在视图控制器中创建按钮按下的方法
(IBAction)detailButton:(UIButton *)发送者{
CustomUITableViewCellName * cell =(CustomUITableViewCellName *)[[sender superview] superview]; NSIndexPath * cellIndexPath = [YOUR_TABLE_NAME indexPathForCell:cell];
}
对于ios 7,添加另一个超级视图。
现在,通过这种方式,您将获得IndexPath,并通过使用indexPath.row或indexPath.section获得当前行。
您可以使用sender.title属性获得标题。
您可以在数组中使用此indexPath来获取所需的详细信息。
您需要分别发送行和节信息,因此请标记为
标签=行* 1000 +部分。
现在从标签获取使用行=标签/ 1000部分=标签%1000的节和行值
使用此值可以制定正确的索引路径,或者找到2分钟左右更好的解决方案来获取该值。
警告其解决方法正确的是
在CustomButton.h中
@interface CustomButton :UIButton
@property (nonatomic,assign)NSIndexPath *path;
@end
在CustomButton.m中
@implementation CustomButton
@synthesize path;
@end
在表格视图单元格上使用此自定义按钮,而不是使用标记集路径属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.