Iphone iOS-以编程方式在TableRow右侧添加ToggleSwitch

Iphone iOS-以编程方式在TableRow右侧添加ToggleSwitch,iphone,ios,uitableview,Iphone,Ios,Uitableview,目前,我正在一行的右侧添加一个图标,并在选择该特定行时切换灯光 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; UITableViewCell *cell = [tableView dequ

目前,我正在一行的右侧添加一个图标,并在选择该特定行时切换灯光

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    ...
    ...
    NSUInteger row = [indexPath row];
    if(row == 3)
    {
        //Draw bulb in row
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bulb-on.png"]];
            //Instead of image, draw toggle-switch here
    }
    else
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [listData objectAtIndex:row];
    return cell;
}

如何绘制可用的切换开关而不是该图像?

Property accessoryView只是一个常用的UIView,因此您可以向其中添加任何子视图:

UISwitch *toggleSwitch = [[UISwitch alloc] init];
cell.accessoryView = [[UIView alloc] initWithFrame:toggleSwitch.frame];
[cell.accessoryView addSubview:toggleSwitch];
另外,别忘了释放分配的对象