iOS - Как программно добавить две строки переключателей в UITableView? - PullRequest
0 голосов
/ 10 октября 2011

Я новичок в разработке 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 & Drink в качестве переключателя и Apparel в качестве ссылки.Что я делаю не так?

1 Ответ

1 голос
/ 10 октября 2011

Одна проблема с вашим подходом состоит в том, что UITableViewCell может иметь только один accessoryView. Возможно, вы захотите создать собственный подкласс UITableViewCell, чтобы добиться того, что вам нужно.

...