开发者

iOS - How to add two rows of switches to a UITableView programmatically?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜