[英]update UITableViewCell appearance when button inside cell is tapped
I have a UITableView with a fairly complex layout, inside it are a plus and a minus sign that allow you to add and subtract values from the cell row. 我有一个UITableView,它的布局相当复杂,里面有一个加号和一个减号,可让您从单元格行中添加和减去值。
Here is my code so far: 到目前为止,这是我的代码:
- (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];
}
And styling the cell: 并设置单元格的样式:
-(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
}
and my code for handling the tap: 和我处理水龙头的代码:
- (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];
}
I was hoping that setting setNeedsLayout would do the redraw but nothing happens,if I simply reload the table this works great but on large amounts of rows it gets very clunky. 我希望设置setNeedsLayout可以重绘,但是什么也没有发生,如果我只是重新加载表,效果很好,但是在大量的行上却变得笨拙。 Also if I scroll this row out of view and then back again it is updated properly. 另外,如果我将此行滚动到视图之外,然后再次返回,则它会正确更新。
How do I get just this row to update without reloading the whole table or having to scroll off and back on the screen again? 我如何只更新该行而无需重新加载整个表或不必滚动并再次在屏幕上返回?
You can have the row update by calling reloadRowsAtIndexPath. 您可以通过调用reloadRowsAtIndexPath来更新行。 You can build the IndexPath based on the cell.tag you're setting in addBtn.tag 您可以基于在addBtn.tag中设置的cell.tag构建IndexPath
NSIndexPath *myip = [[NSIndexPath alloc] indexPathForRow:sender.tag inSection:0];
NSArray *nsa = [[NSArray alloc] initWithObjects:myip, nil];
[thisTableView reloadRowsAtIndexPaths:nsa withRowAnimation:UITableViewRowAnimationFade];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.