我是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&作为开关饮用,服装作为链接。我做错了什么?
答案 0 :(得分:1)
您的方法存在的一个问题是,UITableViewCell
只能有一个accessoryView
。您可能想要创建UITableViewCell
的自定义子类来实现您所追求的目标。