TableviewCell с UIButton - PullRequest
       2

TableviewCell с UIButton

3 голосов
/ 29 марта 2011

Привет ... У меня есть одно табличное представление, в котором каждая ячейка имеет одну кнопку, и я добавляю эту кнопку программно. Теперь я хочу удалить ячейку при нажатии этой кнопки

Кто-нибудь, пожалуйста, помогите мне ...

1 Ответ

1 голос
/ 29 марта 2011

Вот пример, у вас работает надежда:

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


    return rowCount;

}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

    static NSString *cellIdentity = @"removable";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentity];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 70) reuseIdentifier:cellIdentity];
        UIButton *removeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 40)];
        [removeBtn setTitle:@"remove" forState:UIControlStateNormal];
        [removeBtn setBackgroundColor:[UIColor blueColor]];
        [removeBtn addTarget:self action:@selector(removeCell:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:removeBtn];
        [removeBtn release];

    }

    return cell;

}

- (void)removeCell:(id)button {

    UITableViewCell *cell = (UITableViewCell *)[(UIButton *)button superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSArray *removeRow = [NSArray arrayWithObject:indexPath];

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:removeRow withRowAnimation:UITableViewRowAnimationFade];
    rowCount--;

    [self.tableView endUpdates];


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