[英]Adding target/action for custom UITableViewCell
So I can't figure out what I'm doing wrong. 所以我不知道我在做什么错。 I have two cells, each in their own section.
我有两个单元格,每个单元格在各自的部分。 Both have a UISegmentedControl in them with their own outlet/xib, etc. Both rows show up fine in the simulator, but only the first cell (SortByTableViewCell) will have the action called when the UISegmentedControl is pressed.
两者都有一个带有自己的插座/ xib的UISegmentedControl,等等。两行在模拟器中都显示得很好,但是只有第一个单元格(SortByTableViewCell)按下UISegmentedControl时,才会调用该动作。 In the second cell, the UISegmentedControl does not crash the app, but it also does not call its selector.
在第二个单元格中,UISegmentedControl不会使应用程序崩溃,但也不会调用其选择器。 Is there something obvious that I am missing?
有什么明显的我想念的东西吗? Thanks!
谢谢!
- (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;
}
Why setting the addTarget before your cell initialization? 为什么要在单元格初始化之前设置addTarget? Try with:
尝试:
- (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;
}
Hope it's solve your problem. 希望它能解决您的问题。
您是否尝试过在Interface Builder中删除连接并重新创建它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.