обновить внешний вид UITableViewCell при нажатии кнопки внутри ячейки - PullRequest
0 голосов
/ 17 февраля 2012

У меня есть UITableView с довольно сложной компоновкой, внутри него есть знак плюс и минус, который позволяет вам добавлять и вычитать значения из строки ячейки.

Вот мой код:

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

    NSString *CellIdentifier =@"Cell"; //[NSString stringWithFormat: @"Cell%@",[[self.purchaseOrderItems objectAtIndex:indexPath.row] ItemID]] ;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
    } 
    return [self styleCell:cell withIndexPath:indexPath];    
}

И стиль ячейки:

    -(UITableViewCell *) styleCell: (UITableViewCell *) cell withIndexPath: (NSIndexPath *) indexPath {

        cell.tag = indexPath.row;
       // a bunch of layout code goes here along with my button
       UIButton *addBtn = [[[UIButton alloc] initWithFrame:CGRectMake(self.itemsTableView.frame.size.width-247, 35, 38, 33)] autorelease];
       [addBtn setBackgroundImage:[UIImage imageNamed:@"btn-cart-add"] forState:UIControlStateNormal];
       [addBtn setTag:cell.tag];
       [addBtn addTarget:self action:@selector(handleAddTap:) forControlEvents:UIControlEventTouchUpInside];

       // note I have the same tag on the cell as the button

    }

и мой код для обработки крана:

- (void)handleAddTap:(id)sender {  
    UIButton *btn = (UIButton *) sender;
    PurchaseOrderItem *item = [[[PurchaseOrderDataSource sharedPurchaseOrderDataSource] purchaseOrderItems] objectAtIndex:btn.tag];
    [[PurchaseOrderDataSource sharedPurchaseOrderDataSource] AddUpdatePurchaseOrderItem:item.ItemID WithQty:[NSNumber numberWithInt:[item.Qty intValue] + 1 ]];
    UITableViewCell *cell =(UITableViewCell *) [[btn superview ] superview];
   [cell setNeedsLayout];

}

Я надеялся, что установка setNeedsLayout сделает перерисовку, но ничего не произойдет, если я просто перезагружу таблицу, это прекрасно работает, но на большом количестве строк это становится очень неуклюжим.Кроме того, если я прокручиваю эту строку вне поля зрения и затем снова возвращаюсь, она обновляется должным образом.

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

1 Ответ

2 голосов
/ 17 февраля 2012

Вы можете обновить строку, вызвав reloadRowsAtIndexPath. Вы можете построить IndexPath на основе cell.tag, который вы устанавливаете в addBtn.tag

NSIndexPath *myip = [[NSIndexPath alloc] indexPathForRow:sender.tag inSection:0];
NSArray *nsa = [[NSArray alloc] initWithObjects:myip, nil];
[thisTableView reloadRowsAtIndexPaths:nsa withRowAnimation:UITableViewRowAnimationFade];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...