简体   繁体   English

如何确定自定义UITableViewCell中的按钮单击?

[英]How to determine a Button click in a Custom UITableViewCell?

i have a TableView with a Custom Cell and a Section Header. 我有一个带有自定义单元格和节标题的TableView。 The Header shows me the Country and the custom Cell shows me the City. 标头向我显示国家,自定义单元格向我显示城市。

which look like this: 看起来像这样:

USA 美国

  • New York 纽约

  • Los Angeles 洛杉矶

Germany 德国

  • Berlin 柏林

  • Frakfurt 法兰克福

every Cell got a Button. 每个单元都有一个按钮。 after a press it will navigate and push the city name to the detail view . 按下后,它将导航并将城市名称推送到详细信息视图。

The Problem is, that i don't know how to determine the city name after i pressed the button. 问题是,按下按钮后,我不知道如何确定城市名称。

I had a solution but it doesn't work anymore: 我有一个解决方案,但是它不再起作用了:

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

}

when i use my old code it will just show the correct street row in the first section. 当我使用旧代码时,它只会在第一部分中显示正确的街道行。 it didn't detect the other sections(Countries); 它没有检测到其他部分(国家);

When i choose the first city in the thirt section. 当我选择第三部分的第一个城市时。 it will display me the first city in the first section. 它会在第一部分向我显示第一个城市。

I hope you can help me. 我希望你能帮助我。

use this I hope it will solve your purpose. 使用它,希望它能解决您的目的。

Create a button pressed method in your view controller 在视图控制器中创建按钮按下的方法

  • (IBAction)detailButton:(UIButton *)sender { (IBAction)detailButton:(UIButton *)发送者{

    CustomUITableViewCellName *cell = (CustomUITableViewCellName *)[[sender superview]superview]; CustomUITableViewCellName * cell =(CustomUITableViewCellName *)[[sender superview] superview]; NSIndexPath *cellIndexPath = [YOUR_TABLE_NAME indexPathForCell:cell]; NSIndexPath * cellIndexPath = [YOUR_TABLE_NAME indexPathForCell:cell];

} }

For ios 7 add another superview. 对于ios 7,添加另一个超级视图。

Now in this way you will get your IndexPath and you an get the current row by using indexPath.row or indexPath.section. 现在,通过这种方式,您将获得IndexPath,并通过使用indexPath.row或indexPath.section获得当前行。

You can get the title using sender.title property. 您可以使用sender.title属性获得标题。

You can use this indexPath in your array to the required details. 您可以在数组中使用此indexPath来获取所需的详细信息。

You need to send row and section information seperately so make tag as 您需要分别发送行和节信息,因此请标记为

Tag= row*1000+section. 标签=行* 1000 +部分。

now from tag get section and row value using row= tag/1000 section =tag%1000 现在从标签获取使用行=标签/ 1000部分=标签%1000的节和行值

use this value to make correct index path or find some better then 2 mins solution to get that. 使用此值可以制定正确的索引路径,或者找到2分钟左右更好的解决方案来获取该值。

Caution its a workaround correct will be to 警告其解决方法正确的是

  1. Subclass UIButton 子类UIButton
  2. Add a indexpath property int it 在其中添加一个indexpath属性
  3. make your class to make button object and insted of tag set indexpath property. 使您的类成为按钮对象,并插入标记集的indexpath属性。

in CustomButton.h 在CustomButton.h中

@interface CustomButton :UIButton
@property (nonatomic,assign)NSIndexPath *path;
@end

in CustomButton.m 在CustomButton.m中

@implementation CustomButton
@synthesize path;
@end

Use this custom button on your table view cell and instead of tag set path property 在表格视图单元格上使用此自定义按钮,而不是使用标记集路径属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM