【问题标题】:How to find/ delete the CUSTOM row detail of UITableViewCell如何查找/删除 UITableViewCell 的 CUSTOM 行详细信息
【发布时间】:2013-01-09 12:42:15
【问题描述】:

这个问题和我之前的问题有点关系

我是 iPhone 应用程序的新手,正在尝试使用 JSON 学习 UITableView。我关注this video学习了。

我使用 Stoaryboard 创建了相同的示例,还添加了可以添加数据的新屏幕。现在我试图删除数据。所以我所做的就是认为不是下面的方法,而是在每一行上添加按钮,单击该按钮我将删除数据。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

但我不知道如何获取行详细信息以及如何删除。

任何帮助/建议将不胜感激。

下面是我的屏幕的样子。

注意:

UITableViewCell 是 CUSTOM 类型。

【问题讨论】:

    标签: iphone objective-c uitableview ios6


    【解决方案1】:

    使用以下代码根据编辑操作删除更新数据模型。

    此波纹管代码只是一个示例,用于从数组和表中热删除或删除对象..有关更多信息,请查看我在下面发布链接的教程..

    - (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
         if (editingStyle == UITableViewCellEditingStyleDelete) {
    
             [arryData removeObjectAtIndex:indexPath.row];
    
            [tblSimpleTable reloadData];
    
         } 
    }
    

    或者也使用此波纹管逻辑与单元格的自定义按钮...

    - (IBAction)deleteCustomCellWithUIButton:(id)sender
    {
      NSIndexPath *indexPath = [yourTableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
      NSUInteger row = [indexPath row];
      [yourTableView removeObjectAtIndex:row];
      [yourTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];
    }
    

    有关更多信息,请参阅这些教程...

    1. iphone-sdk-tutorial-add-delete-reorder-UITableView-row

    2. multiple-row-selection-and-editing-in

    【讨论】:

    • 谢谢帕拉斯。如果有任何疑问,我会检查并回复您。
    • 我正在从实时数据库中提取数据。所以我相信这行不通。我相信,当我单击第 0 行时,我应该获取该行的 id,以便我可以使用该 Id 删除数据。同样为此,我需要在添加删除按钮时设置标签。如果我错了,请纠正我...
    • @FahimParkar 嘿,这里不需要为此创建一些特殊代码,这里 roland 说也在这里,通过这个示例,您可以了解如何在运行时添加或删除 raw,但您也可以也从现场删除数据..只需在此处输入一些逻辑,您就可以得到它..
    • @FahimParkar 在这里,您只需从网络中删除数据,就像您从数组中删除数据一样,如果您使用自定义按钮删除原始数据,您还可以使用标签实现逻辑.. 也可以在 @987654323 中查找@这个链接希望这对你有帮助伙计.. :)
    • @FahimParkar 我更新了关于如何从自定义按钮点击事件中删除原始数据和数据的答案... :)
    【解决方案2】:

    您可以在每一行上添加一个按钮,并在cellForRowAtIndex: 方法中将button.tag 设置为indexPath.row。然后在您的按钮方法中,您可以从用于填充UITableView 然后reloadDataArray 中删除条目

    【讨论】:

    • 你能给我这个例子的链接吗?
    • 这个没有链接,你要自己编码,如果需要更多信息和不清楚的地方请告诉我
    • 好的,我明白了你的意思,我正在执行相同的操作。感谢您显示路径:)
    • 不客气 :) 如果您喜欢这个答案,请点赞并采纳,谢谢!
    【解决方案3】:

    你有很多方法来获取单元格或单元格的索引路径:

    • (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
    • (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
    • (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
    • (NSArray *)indexPathsForRowsInRect:(CGRect)rect
    • (NSArray *)visibleCells
    • (NSArray *)indexPathsForVisibleRows

    如果你想处理 UIButton 的 IBAction 而不是像这样使用 indexPathForRowAtPoint :

    -(IBAction)myAction:(id)sender
    {
    
    CGPoint location            = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath      = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *swipeCell  = [self.tableView cellForRowAtIndexPath:indexPath];
    
    NSLog(@"Selected row: %d", indexPath.row);
    //......
    
    }
    

    OR indexPathForCell

    - (void)buttonPressedAction:(id)sender
    {
       UIButton *button = (UIButton *)sender;
       // Get the UITableViewCell which is the superview of the UITableViewCellContentView which is the superview of the UIButton
       (UITableViewCell*)cell = [[button superview] superview];
       int row = [myTable indexPathForCell:cell].row;
    }
    

    我已从以下位置获取代码: Detecting which UIButton was pressed in a UITableView

    Custom UITableViewCell button action?

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多