Как динамически добавить UIButton в UITableViewCell? - PullRequest
0 голосов
/ 26 декабря 2011

Я хочу динамически добавить UIB-кнопку в ячейку таблицы - см. Требования ниже.

Когда пользователь выбирает ячейку, я просто настраиваю высоту этой ячейки таблицы в

-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath {
        selIndPath = [indexPath retain];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
}

Вышеуказанный метод заставляет tableView регулировать высоту выбранной строки. Но когда я добавил UIButton вышеописанным способом, кнопка не видна.

Меня не интересует [self.tableView reloadData];

Любая помощь с благодарностью.

***** EDITED *******

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (selIndPath && selIndPath == indexPath) {
        //UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
//      UIButton* btn = (UIButton*)[[cell contentView] viewWithTag:1234];
//      btn.frame = CGRectMake(40, 60, 60, 24);
//      [cell setNeedsDisplay];
        return 90;
    }
    return 64;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [[cell contentView] setBackgroundColor:[UIColor clearColor]];
        [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
    }

    // THIS LINES OF CODE WORKS ONLY ON reloadData
    if (selIndPath && selIndPath == indexPath) {
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(40, 60, 60, 24);
        btn.tag = 1234;
        [btn setTitle:@"Hi" forState:UIControlStateNormal];
        [cell.contentView addSubview:btn];
    }

    // Configure the cell.
    cell.textLabel.text = @"Loading..";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selIndPath = [indexPath retain];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    //[self.tableView reloadData];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

1 Ответ

2 голосов
/ 26 декабря 2011

Добавление кнопки не будет работать, если вы добавите ее в ячейку в cellForRowAtIndexPath:.Что вам нужно сделать, это создать пользовательский UITableViewCell (см. Ответ на этот вопрос, как- Изменение позиции пользовательского UIButton в пользовательском UITableViewCell )

В пользовательской ячейке создайтеМетод

-(void) addButtonToCell
{
   UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(40, 60, 60, 24);
    btn.tag = 1234;
    [btn setTitle:@"Hi" forState:UIControlStateNormal];
    [self.contentView addSubview:btn];

}

Конечно, то, как вы добавляете кнопку и какие аргументы вы передаете этому методу, зависит от вас, но вы получаете суть.

В cellForRowAtIndexPath: просто добавьте

if (selIndPath && selIndPath == indexPath) {
[cell addButtonToCell];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...