简体   繁体   中英

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. In the second cell, the UISegmentedControl does not crash the app, but it also does not call its selector. 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? 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中删除连接并重新创建它?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM