【问题标题】:Update UILabel within a UITableViewCell在 UITableViewCell 中更新 UILabel
【发布时间】:2015-09-12 21:10:56
【问题描述】:

当点击单元格内的按钮时,我正在尝试更新单元格内的 UILabel。我将如何处理这个问题。这是我创建 UITableViewCell 的代码:

已编辑:按 TAP 方法

- (void)likeButtonTap:(InstagramLikeButton *)sender {
    [sender setSelected:!sender.selected];
    // Set objectID
    NSString *objectID = sender.objectID;
    int *likesCount = sender.likesCount;
    likesCount = likesCount + 1;
    NSString *likesString = [NSString stringWithFormat:@"%d",likesCount];
    // Get Cell
    NSIndexPath *i=[self indexPathForCellContainingView:sender.superview];
    InstagramCell *cell = (InstagramCell*)[self.tableView cellForRowAtIndexPath:i];
    cell.likeLabel = likesString;

    NSString *path = [NSString stringWithFormat:@"media/%@/likes", objectID];
    [[InstagramClient sharedClient] postPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully Liked Picture");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", error);
    }];
    [sender increaseLikeCount:sender];
    NSLog(@"Successfully Liked Picture");
}

这是我收到的错误:

Floadt[2669:176980] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString setText:]: unrecognized selector sent to instance 0x7f7f7a8bda50'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000107541c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000106c7ebb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001075490ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010749f13c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010749ecd8 _CF_forwarding_prep_0 + 120
    5   Floadt                              0x00000001036d455f -[InstagramTableViewController tableView:cellForRowAtIndexPath:] + 3663
    6   UIKit                               0x0000000105ab3a28 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
    7   UIKit                               0x0000000105a92248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
    8   UIKit                               0x0000000105aa88a9 -[UITableView layoutSubviews] + 210
    9   UIKit                               0x0000000105a32a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    10  QuartzCore                          0x00000001057f6ec2 -[CALayer layoutSublayers] + 146
    11  QuartzCore                          0x00000001057eb6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    12  QuartzCore                          0x00000001057eb546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    13  QuartzCore                          0x0000000105757886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    14  QuartzCore                          0x0000000105758a3a _ZN2CA11Transaction6commitEv + 462
    15  QuartzCore                          0x000000010581a075 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 489
    16  CoreFoundation                      0x00000001074a9174 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    17  CoreFoundation                      0x00000001074a8d35 __CFRunLoopDoTimer + 1045
    18  CoreFoundation                      0x000000010746ad3d __CFRunLoopRun + 1901
    19  CoreFoundation                      0x000000010746a366 CFRunLoopRunSpecific + 470
    20  GraphicsServices                    0x0000000109029a3e GSEventRunModal + 161
    21  UIKit                               0x00000001059b2900 UIApplicationMain + 1282
    22  Floadt                              0x000000010370ba6f main + 111
    23  libdyld.dylib                       0x000000010977e145 start + 1
    24  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

标签: ios objective-c iphone uitableview uilabel


【解决方案1】:

您可以通过CGPoint使用locationInView:轻松获取一行的indexPath。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(incrementLikes:)];
tap.numberOfTapsRequired = 1;
[cell.someLabel addGestureRecognizer:tap];

-(void)incrementLikes:(UITapGestureRecognizer *)gesture {
    CGPoint location = [gesture locationInView:self.tableView];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:location];
    NSInteger indexPathForLabel = tappedIndexPath.row;

    //do whatever now that you have the indexPath for respective cell
}

【讨论】:

    【解决方案2】:
    [tbl beginUpdates];
    [tbl reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    [tbl endUpdates];
    

    【讨论】:

    • 请在您的回答中添加一些描述。
    【解决方案3】:

    我通常给按钮一个tag,它类似于UITableViewCellindexPath.row

    然后您可以轻松地向UITableView 询问正确的单元格:

    InstagramCell *cell = (InstagramCell*)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
    

    【讨论】:

      【解决方案4】:

      我会说您可以通过发送者的superview 属性获取单元格槽 ,然后分配单元格的任何属性都是微不足道的

      InstagramCell *cell = ([[sender superview] superview] as InstagramCell);
      [cell.likeLabel setText:likes];
      

      【讨论】:

      • 这种情况下UIButton的superview不就是UITableViewCell的contentView吗?
      • @Jasper:你是 100% 正确的。我需要一个额外的 superview 调用来获取单元格
      【解决方案5】:

      我猜你需要获取当前活动单元格,点击喜欢的按钮,
      要获取当前单元格引用,只需使用..

      -(IBAction)likeButtonTapped:(id)sender {
      
          NSIndexPath *i=[self indexPathForCellContainingView:sender.superview];
          UITableViewCell *cell=(UITableViewCell*)[tableMain cellForRowAtIndexPath:i];
      
          // use this cell reference for you'r work...
      }
      
      // Here is the key method...
      
      - (NSIndexPath *)indexPathForCellContainingView:(UIView *)view {
          while (view != nil) {
              if ([view isKindOfClass:[UITableViewCell class]]) {
                  return [tableMain indexPathForCell:(UITableViewCell *)view];
              } else {
                  view = [view superview];
              }
          }
      
          return nil;
      }
      

      【讨论】:

      • 我已尝试使用您的方法来尝试增加我的 Instagram Cell 上的点赞数。但是,每次我运行应用程序时都会收到错误消息,我编辑了我的问题以包含我更新的代码。
      • 请参阅,我仅使用“indexPathForCellContainingView”方法获得单元格引用...供您参考,在我的情况下,我在 UITableView 中有不同的部分。单节有 uitextfields。现在在 '-(BOOL)textFieldShouldBeginEditing:(UITextField )textField { NSIndexPath *i=[self indexPathForCellContainingView:textField.superview]; UITableViewCell *cell=(UITableViewCell)[tableMain cellForRowAtIndexPath:i]; '
      • 继续之前的评论..从上面的“单元格”参考我得到了哪个 UITextField 处于编辑模式,就是这样!!!现在你检查你在哪一行得到错误,我猜是在 setText 时。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      相关资源
      最近更新 更多