iOS - 如何以编程方式将两行开关添加到UITableView?

时间:2011-10-10 19:29:31

标签: ios uitableview uiswitch

我是iOS开发新手。我试图使用以下代码以编程方式将两个开关添加到表中 -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section== 0){

    fooddrinkSettingSwitch=[[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];


    [fooddrinkSettingSwitch addTarget:self action:@selector(preferenceSettingAction:) forControlEvents:UIControlEventValueChanged];
    [cell addSubview:fooddrinkSettingSwitch];
    cell.accessoryView = fooddrinkSettingSwitch;

    apparelSettingSwitch=[[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];


    [apparelSettingSwitch addTarget:self action:@selector(preferenceSettingAction:) forControlEvents:UIControlEventValueChanged];
    [cell addSubview:apparelSettingSwitch];
    cell.accessoryView = apparelSettingSwitch;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;    
}


//get the dictionary object
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"SettingTitle"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
}

然而,它显示了Food&作为开关饮用,服装作为链接。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的方法存在的一个问题是,UITableViewCell只能有一个accessoryView。您可能想要创建UITableViewCell的自定义子类来实现您所追求的目标。