[英]Adding target/action for custom UITableViewCell
所以我不知道我在做什么错。 我有两个单元格,每个单元格在各自的部分。 两者都有一个带有自己的插座/ xib的UISegmentedControl,等等。两行在模拟器中都显示得很好,但是只有第一个单元格(SortByTableViewCell)按下UISegmentedControl时,才会调用该动作。 在第二个单元格中,UISegmentedControl不会使应用程序崩溃,但也不会调用其选择器。 有什么明显的我想念的东西吗? 谢谢!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
if (section == 0) {
static NSString *SortByCellIdentifier = @"SortByCellIdentifier";
SortByTableViewCell *cell = (SortByTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SortByCellIdentifier];
[cell.SortBySegmentedControl addTarget:self action:@selector(SortBySegmentedControlPressed:) forControlEvents:UIControlEventValueChanged];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"SortByTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[SortByTableViewCell class]]) {
cell = (SortByTableViewCell *)currentObject;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
else {
static NSString *ConditionCellIdentifier = @"ConditionCellIdentifier";
ConditionTableViewCell *cell = (ConditionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ConditionCellIdentifier];
[cell.ConditionSegmentedControl addTarget:self action:@selector(ConditionSegmentedControlPressed:) forControlEvents:
UIControlEventValueChanged];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"ConditionTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[ConditionTableViewCell class]]) {
cell = (ConditionTableViewCell *)currentObject;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
为什么要在单元格初始化之前设置addTarget? 尝试:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
if (section == 0) {
static NSString *SortByCellIdentifier = @"SortByCellIdentifier";
SortByTableViewCell *cell = (SortByTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SortByCellIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"SortByTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[SortByTableViewCell class]]) {
cell = (SortByTableViewCell *)currentObject;
}
}
}
[cell.SortBySegmentedControl addTarget:self action:@selector(SortBySegmentedControlPressed:) forControlEvents:UIControlEventValueChanged];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
else {
static NSString *ConditionCellIdentifier = @"ConditionCellIdentifier";
ConditionTableViewCell *cell = (ConditionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ConditionCellIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"ConditionTableViewCell" owner:self options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[ConditionTableViewCell class]]) {
cell = (ConditionTableViewCell *)currentObject;
}
}
}
[cell.ConditionSegmentedControl addTarget:self action:@selector(ConditionSegmentedControlPressed:) forControlEvents:
UIControlEventValueChanged];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
希望它能解决您的问题。
您是否尝试过在Interface Builder中删除连接并重新创建它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.