0

有人知道如何检查哪些分段控件触发了事件吗?我正在创建两个包含分段控件的单元格。在方法 segmentedControlIndexChanged 中,我想知道哪个分段 Control 触发了事件。我使用了标签,但它不起作用。我对此很陌生。我会很感激你的帮助。

包含分段控件的单元格代码:

 cellParinti = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cellParinti.textLabel.text = @"Parinti";
cellParinti.label.text = dog?dog.parinti:@"";
cellParinti.segmentedControl.selectedSegmentIndex = [dog.parinti isEqualToString:@"da"] ? 0 : 1;
cellParinti.segmentedControl.tag = 1;
[cellParinti.segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
[cellParinti addSubview:cellParinti.textLabel];
[cellParinti addSubview:cellParinti.label];
[cellParinti addSubview:cellParinti.segmentedControl];


cellPedigree = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cellPedigree.textLabel.text = @"Pedigree";
cellPedigree.label.text = dog?dog.pedigree:@"";
cellParinti.segmentedControl.tag = 2;
cellPedigree.segmentedControl.selectedSegmentIndex = [dog.pedigree isEqualToString:@"da"] ? 0 : 1;
[cellPedigree.segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];

[cellPedigree addSubview:cellPedigree.textLabel];
[cellPedigree addSubview:cellPedigree.label];
[cellPedigree addSubview:cellPedigree.segmentedControl];

该方法的代码已被触发:

-(void) segmentedControlIndexChanged:(id)sender{
NSIndexPath *indexPath;

UISegmentedControl * sg1 = (UISegmentedControl *)sender;

// reload data based on the new index
if (sg1.tag == 1){
cellParinti.label.text = sg1.selectedSegmentIndex == 0 ? @"da" : @"nu";
dog.parinti = sg1.selectedSegmentIndex == 0 ? @"da" : @"nu";
}
else {
    cellPedigree.label.text = sg1.selectedSegmentIndex == 0 ? @"da" : @"nu";
    dog.pedigree = sg1.selectedSegmentIndex == 0 ? @"da" : @"nu";
}

}

4

1 回答 1

1

你有

cellParinti.segmentedControl.tag = 2;

我认为这应该是

cellPedigree.segmentedControl.tag = 2;

这是您的代码中的一个错字,这会阻止您获得正确的标签。

于 2012-11-06T15:13:28.523 回答