简体   繁体   English

轻按单元格内的按钮时更新UITableViewCell外观

[英]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.

相关问题 在DetailViewController中点击按钮时,在单个UITableViewCell内部更改图像 - Change image inside of a single UITableViewCell when button tapped in DetailViewController 轻按tableview单元格内的按钮时,触发UITableViewCellEditingStyleDelete - Trigger UITableViewCellEditingStyleDelete when button inside tableview cell is tapped 轻按单元格时,从uitableviewcell类调用uipangesture - Calling uipangesture from uitableviewcell class when tapped a cell 轻敲单元格时,更改表格单元格内UILabel的颜色 - Change the color of UILabel inside table cell when cell is tapped 当点击单元格上的按钮时,将信息写入单元格中 - Take Information Writter In A Cell When Button On A Cell Tapped 轻按另一个时在表格视图单元格中取消选择一个按钮 - Deselect a button in a table view cell when another is tapped 如何知道iOS 6 UITableViewCell上是否点击了Delete按钮 - How to know if Delete button is tapped on iOS 6 UITableViewCell 当它是 UITableViewCell 内视图的子视图时,按钮在 UITableViewCell 子类中不起作用 - Button not working in UITableViewCell subclass when it is a subview of a view inside the UITableViewCell 按下UITableViewCell中的Button时的scrollToRowAtIndexPath - scrollToRowAtIndexPath when Button inside UITableViewCell is pressed 轻按按钮时,包含按钮的表视图单元格似乎始终具有索引0 - Table view cell containing button always seems to have index 0 when button is tapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM