Когда переключатель выбора флажка - PullRequest
1 голос
/ 01 июля 2011

Я использую переключатель на ячейке UITableView. Во-первых, переключатель не отмечен. Когда радио кнопка проверена. Я прокручиваю UITableView, кнопка радио не проверена. Используя этот код ниже

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return 100;

}

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

    static NSString *CellIdentifier    = @"Cell";
    static NSString *CellIdentifierFirst    = @"CellFirst";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFirst];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
    }

    NSArray *cellSubs = cell.contentView.subviews;
    for (int i = 0 ; i < [cellSubs count] ; i++) {
        [[cellSubs objectAtIndex:i] removeFromSuperview];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIButton *redioBtn1 = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 20, 20)]; 
    [redioBtn1 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
    [redioBtn1 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
    redioBtn1.tag = ++tagCount;
    [cell.contentView addSubview:redioBtn1];

    UIButton *redioBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(80, 20, 20, 20)]; 
    [redioBtn2 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
    [redioBtn2 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
    redioBtn2.tag = ++tagCount;
    [cell.contentView addSubview:redioBtn2];

    UIButton *redioBtn3 = [[UIButton alloc]initWithFrame:CGRectMake(140, 20, 20, 20)]; 
    [redioBtn3 setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
    [redioBtn3 addTarget:self action:@selector(selectRadioButon:) forControlEvents:UIControlEventTouchUpInside];
    redioBtn3.tag = ++tagCount;
    [cell.contentView addSubview:redioBtn3];

    return cell;


}


-(void)selectRadioButon:(id)sender {


    btnTag = [sender tag];  
    NSArray *arr = self.view.subviews; 
    UITableView *tblCell = [arr objectAtIndex:0];
    NSArray *cellAry = tblCell.subviews;

    for (int i = 0; i <[cellAry count]; i++) {
        UITableViewCell *content = [cellAry objectAtIndex:i];
        NSArray *contentArry = content.contentView.subviews;
        for (int j = 0; j <[contentArry count]; j++) {
            UIButton *button = [contentArry objectAtIndex:j];
            if (btnTag == button.tag) {
                for (int i = 0; i <[contentArry count]; i++) {
                    UIButton *button = [contentArry objectAtIndex:i];
                    if (btnTag == button.tag) {
                        if ([sender currentImage]==[UIImage imageNamed:@"radioselect.png"]) 

                            [button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];
                        else
                            [button setImage:[UIImage imageNamed:@"radioselect.png"] forState:UIControlStateNormal];
                    }
                    else
                        [button setImage:[UIImage imageNamed:@"radiounselect.png"] forState:UIControlStateNormal];

                }

                return;
            }
        }

    }


}

1 Ответ

0 голосов
/ 01 июля 2011

Первое: Вы не должны добавлять UIButton при каждой загрузке ячейки. Вы должны добавить их внутрь if (cell == nil) block.

Следующая вещь: Необходимо отслеживать выбранный индекс переключателя (значение int ) в отдельном массиве (a NSMutableArray ) и выбрать соответствующая кнопка UIB, за пределами if (cell == nil) block.

Надеюсь этот урок поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...