2012-02-17 73 views
0

我有一個相當複雜的佈局的UITableView,裏面有一個加號和一個減號,允許你從單元格行加減值。更新UITableViewCell外觀,當單元格內的按鈕被挖掘

這是到目前爲止我的代碼:

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

    NSString *CellIdentifier [email protected]"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會做重繪,但沒有任何反應,如果我只是重新加載表,這個工程很好,但在大量的r它變得非常笨重。此外,如果我滾動此行的視圖,然後再回來它正確更新。

如何在沒有重新加載整個表格的情況下更新此行,或者不必在屏幕上再次滾動顯示?

回答

2

您可以通過調用reloadRowsAtIndexPath來更新行。你可以建立基於該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]; 
+0

完美設置IndexPath,但構造是這樣的:NSIndexPath * MYIP = [NSIndexPath indexPathForRow:sender.tag切入口:0]; – Slee 2012-02-17 11:11:39