【发布时间】:2025-05-09 02:40:02
【问题描述】:
我有一个布局相当复杂的 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 会进行重绘,但没有任何反应,如果我只是重新加载表格,这效果很好,但在大量行上它会变得非常笨重。此外,如果我将此行滚动到视图之外,然后再次返回,它会正确更新。
如何在不重新加载整个表格或不必再次滚动并返回屏幕的情况下仅更新这一行?
【问题讨论】:
标签: objective-c ios uitableview