有人知道如何检查分段控件触发了什么事件吗?我正在创建两个包含分段控件的单元格。在segmentedControlIndexChanged方法中,我想知道哪个分段控件已触发事件。我使用标签,但它不起作用。我是新来的。我很感激你的帮助。
单元格的代码,包含分段控件:
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";
}
}
答案 0 :(得分:1)
你有
cellParinti.segmentedControl.tag = 2;
我认为这应该是
cellPedigree.segmentedControl.tag = 2;
这是代码中的拼写错误,导致您无法获取正确的代码。