在UITableViewCell中更新UILabel

时间:2015-06-26 03:13:36

标签: ios objective-c iphone uitableview uilabel

当我点击单元格中的按钮时,我正在尝试更新单元格内的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

5 个答案:

答案 0 :(得分:0)

我会说你可以获得发件人的单元格InstagramCell *cell = ([[sender superview] superview] as InstagramCell); [cell.likeLabel setText:likes]; 属性 ,然后分配单元格的任何属性是微不足道的

{{1}}

答案 1 :(得分:0)

您可以使用locationInView:通过CGPoint轻松获取行的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 :(得分:0)

[tbl beginUpdates];
[tbl reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
[tbl endUpdates];

答案 3 :(得分:0)

我通常会给按钮tag一个类似于indexPath.row的{​​{1}}。

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

UITableView

答案 4 :(得分:0)

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

-(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;
}