1

I am trying to update a UILabel within a Cell when a button within the cell is tapped. How would I go on as to approaching this. Here is my code for the creation of the UITableViewCell:

EDITED: On TAP Method

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

Here is the error I am receiving:

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
Prad
  • 469
  • 1
  • 8
  • 28
  • Add an [Exception breakpoint](http://stackoverflow.com/a/17802723/2857130) and tell us which line of code the app crashes at – staticVoidMan Jun 28 '15 at 18:23

5 Answers5

0

I'd say you could get the cell trough superview property of the sender , then assigning any property of the cell is trivial

InstagramCell *cell = ([[sender superview] superview] as InstagramCell);
[cell.likeLabel setText:likes];
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
0

You can easily get the indexPath of a row using locationInView: through CGPoint.

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
}
soulshined
  • 9,612
  • 5
  • 44
  • 79
0
[tbl beginUpdates];
[tbl reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
[tbl endUpdates];
Om Prakash
  • 9,191
  • 1
  • 12
  • 20
0

I usually give the button a tag which resembles the indexPath.row of the UITableViewCell.

Then you can easily ask the UITableView for the correct cell:

InstagramCell *cell = (InstagramCell*)[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
Jasper
  • 7,031
  • 3
  • 35
  • 43
0

I guess you need to get the current active cell, where the liked button is clicked,
To get current cell reference, just use..

-(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;
}
Jasper
  • 7,031
  • 3
  • 35
  • 43
Vinod Supnekar
  • 143
  • 1
  • 7
  • I have attempted to use your method to try to increment the likes count on my Instagram Cell. However I am receiving an error every time I run the application I edited my question to include my updated code. – Prad Jun 28 '15 at 17:44
  • see , i have got cell reference with " indexPathForCellContainingView " method only...for your reference ,in my case, I have different Sections in UITableView. Single section had uitextfields. Now on '-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { NSIndexPath *i=[self indexPathForCellContainingView:textField.superview]; UITableViewCell *cell=(UITableViewCell*)[tableMain cellForRowAtIndexPath:i]; ' – Vinod Supnekar Jun 29 '15 at 05:45
  • continued from previous comment.. from above 'cell' ref i got which UITextField was in editing mode ,that's it!!! Now you check at which line you get Error, i guess it is while setText. – Vinod Supnekar Jun 29 '15 at 05:54