为自定义UITableViewCell添加目标/操作

时间:2011-10-31 15:37:49

标签: iphone

所以我无法弄清楚我做错了什么。我有两个单元格,每个单元格都有自己的部分。两者都有一个带有自己的outlet / 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;
       }

2 个答案:

答案 0 :(得分:1)

为什么在单元初始化之前设置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;
   }

希望它能解决你的问题。

答案 1 :(得分:0)

您是否尝试在Interface Builder中删除连接并重新创建它?