Iphone 添加到单元格的UISegmentController

Iphone 添加到单元格的UISegmentController,iphone,objective-c,Iphone,Objective C,我有一个tableView,其中有一个UISegmentController。因此每个单元格都有一个UISegmentController 当用户单击(给定单元格的)UISegmentController中的某个段时,我如何知道单击了哪个单元格?我需要记录该单元格的标题,我如何才能做到这一点(注意:用户只会单击单元格的UISegmentController,而不会单击单元格本身) 以下代码是单击UISegmentController时将调用的方法 -(void)segmentOfCellPres

我有一个tableView,其中有一个UISegmentController。因此每个单元格都有一个UISegmentController

当用户单击(给定单元格的)UISegmentController中的某个段时,我如何知道单击了哪个单元格?我需要记录该单元格的标题,我如何才能做到这一点(注意:用户只会单击单元格的UISegmentController,而不会单击单元格本身)

以下代码是单击UISegmentController时将调用的方法

-(void)segmentOfCellPressed:(id)sender{

}

应该很容易。UISegmentedController从派生UIView的UIControl派生。UIView对象具有标记属性。创建每个UISegmentedController时,为每个UISegmentedController指定一个唯一的标记值。您可能需要在字典中分别管理哪些标记值

-(void)segmentOfCellPressed:(id)sender
{
   UISegmentedController *segmentedController = (UISegmentedController *)sender;
   UITableViewCell *cell = [self cellFromTag:segmentedController.tag];  
}

您需要创建一个名为cellFromTag的方法,该方法将从UISegmentedController的标记值返回单元格。如果您不想要单元格,而是想要其他内容,那么您可以将其返回。

应该很容易。UISegmentedController从派生UIView的UIControl派生。UIView对象具有标记属性。创建每个UISegmentedController时,为每个UISegmentedController指定一个唯一的标记值。您可能需要在字典中分别管理哪些标记值

-(void)segmentOfCellPressed:(id)sender
{
   UISegmentedController *segmentedController = (UISegmentedController *)sender;
   UITableViewCell *cell = [self cellFromTag:segmentedController.tag];  
}
您需要创建一个名为cellFromTag的方法,该方法将从UISegmentedController的标记值返回单元格。如果您不想要单元格,而是想要其他内容,则可以将其返回。

尝试以下操作:

-(void)segmentOfCellPressed:(id)sender{
    UISegmentController *segmentController = (UISegmentController *)sender;

    YourCellClass *cell=(YourCellClass *)segmentController.superview.superview;  // your clicked cell
 // or a little bit more verbose but imho easier to understand:
 // UIView *contentView = [segmentController superview];
 // YourCellClass *cell = (YourCellClass *)[contentView superview];

    NSIndexPath *path = [yourTableView indexPathForCell:cell]; //indexPath of clicked cell

}
试试这个:

-(void)segmentOfCellPressed:(id)sender{
    UISegmentController *segmentController = (UISegmentController *)sender;

    YourCellClass *cell=(YourCellClass *)segmentController.superview.superview;  // your clicked cell
 // or a little bit more verbose but imho easier to understand:
 // UIView *contentView = [segmentController superview];
 // YourCellClass *cell = (YourCellClass *)[contentView superview];

    NSIndexPath *path = [yourTableView indexPathForCell:cell]; //indexPath of clicked cell

}

您可以将段控制器的tag属性设置为cellForRowAtIndexPath中单元格的索引,然后在选择器中查询段控制器,然后通过标记获取单元格:

UISegmentController *segmentController = (UISegmentController*)sender;
int row = segmentController.tag;
希望这能有所帮助

在将UISegmentController添加到单元格的cellForRowAtIndexPath方法中,执行以下操作:

segment.tag = indexPath.row;

您可以将段控制器的tag属性设置为cellForRowAtIndexPath中单元格的索引,然后在选择器中查询段控制器,然后通过标记获取单元格:

UISegmentController *segmentController = (UISegmentController*)sender;
int row = segmentController.tag;
希望这能有所帮助

在将UISegmentController添加到单元格的cellForRowAtIndexPath方法中,执行以下操作:

segment.tag = indexPath.row;

如何添加SegmentController?我已将SegmentController添加到单元格中。操作设置如下
[segment addTarget:self action:@selector(segmentOfCellPressed:)forControlEvents:UIControlEventValueChanged]如何添加SegmentController?我已将SegmentController添加到单元格中。操作设置如下
[segment addTarget:self action:@selector(segmentOfCellPressed:)forControlEvents:UIControlEventValueChanged]我也在想同样的事情!不要忘了考虑一个很好的算法,将由行和节组成的indexPath转换为单个数字。@Illep segmentedController.tag=tagValue。请记住,每个单元都被重用,因此每个单元中的分段控制器也将被重用。每次调用[tableView:cellforIndexPath:]时,您都需要更新segmentedController值和标记。我也这么想!不要忘了考虑一个很好的算法,将由行和节组成的indexPath转换为单个数字。@Illep segmentedController.tag=tagValue。请记住,每个单元都被重用,因此每个单元中的分段控制器也将被重用。每次调用[tableView:cellforIndexPath:]时,您都需要更新segmentedController值和标记。根据实际的单元格布局,您必须更改superview的数量。如果线段是单元格内容视图的直接子视图,则答案非常有效。根据实际单元格布局,您必须更改superview的数量。如果片段是单元格contentView的直接子视图,则答案非常有效。