I am an开发者_开发百科 iOS development newbie. I am trying to add two switches to a table programmatically using the following code -
- (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;
}
However, it displays Food & Drink as a switch and Apparel as a link. What am I doing wrong?
One problem with your approach is that a UITableViewCell
can have only one accessoryView
. You probably want to make a custom subclass of UITableViewCell
to achieve what you're after.
精彩评论