質問

I am trying to build a UITableViewCell that looks like this:

Since I can't post an image yet I'll try to describe it by saying it's a label (left) with a UISwitch (middle) and the accessory (right).

Hope ya'll get the picture...

The idea is that the accessoryView is visible but disabled if the switch is off. When the user turns on the switch then they can tap and navigate right to see the list of options that they can select. Trouble is, when the switch is tapped, the cell gets the tap not the switch.

What I gotta' do? (to make the switch get the tap first). I'm guessing it's a firstResponder thing but I'm not finding the magic code that I need.

Once I get past this I can probably figure out the enable/disable of the accessory my self...

Thanks.

役に立ちましたか?

解決

Create UISwitch control and add it to the cell content view.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = (UITableViewCellAccessoryNone;
            UISwitch* aSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
aSwitch.tag = [indexPath row];

            CGRect rect = cell.frame;
            CGRect switchRect = aSwitch.frame;
            switchRect.origin = CGPointMake( (rect.size.width / 2) - (aSwitch.frame.size.width / 2), 
                                               (rect.size.height / 2) - (aSwitch.frame.size.height / 2));

            aSwitch.frame = switchRect;
            [aSwitch addTarget:self action:@selector(switchSwitched) forControlEvents:UIControlEventValueChanged];
            [cell addSubview:aSwitch];


            [aSwitch release];
        }

        return cell;
    }

    - (void)switchSwitched:(UISwitch*)sender {

        if (sender.on) {

    UITableViewCell* aCell = [self.tableView cellForRowAtIndexPath:
                              [NSIndexPath indexPathForRow:sender.tag inSection:0]];

    aCell.accessoryType = (sender.on == YES ) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
        }
    }

You can also implement this differently by Subclassing UITableViewCell and adding UITableViewCell nib file. Make the UIViewTableController the file owner of the Cell nib file, add to the UIViewController a IBOutlet for the subclassed cell. Load the custom cell using

        [[NSBundle mainBundle] loadNibNamed:@"Your Custom Cell nib file name" owner:self options:nil];

see Apple programming guide for iOS http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top