0

I have a UITableView inside my UIViewController. Data, subtitle data and and accessory buttons all load fine; however, when I press any accessory button they all return index 0? dataSource and delegate are wired to File's Owner in the .xib What am I missing?

Header file:

@interface OrderPage : UIViewController <UITableViewDataSource, UITableViewDelegate>  {
    NSDictionary *userOrderData;
    NSMutableArray *myMasterList;
    UITableView *tableView;
    NSMutableArray *contentss;
    NSMutableArray *mysendArray;
    NSDictionary *my_data;

}

-(IBAction)sendOrders;
@property( nonatomic, retain) NSDictionary *userOrderData;
@property ( nonatomic, retain) IBOutlet UILabel *firstOrder;
@property(nonatomic, retain) NSMutableArray *myMasterList;
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *contentss;
@property(nonatomic, retain) NSMutableArray *mysendArray;
@property(nonatomic, retain) NSDictionary *my_data;
@end

Implementation, my cellForRowAtIndexPath: (button section):

@synthesize tableView = _tableView;

UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.tag = indexPath.row;
[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

My buttonPressed method:

- (void)checkButtonTapped:(UIButton *)sender {
    UITableViewCell *cell = ((UITableViewCell *)[sender superview]);

    NSLog(@"cell row: int%i", [self.tableView indexPathForCell:cell].row);
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
    NSLog(@"The row id is %d",  indexPath.row);  

    UIImageView *btnCliked =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_checkmark.png"]];
    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [activityView startAnimating];
    cell.accessoryView = btnCliked;
}

All of this code works on my other dedicated UITableView pages.

jscs
  • 63,694
  • 13
  • 151
  • 195
AhabLives
  • 1,308
  • 6
  • 22
  • 34
  • Wouldn't it be easier to set this value on the button itself in your `tableView:cellForRowAtIndexPath:`? If you have to, you could just subclass UIButton and add a custom `NSIndexPath` property. Seems easier and more reliable than going to the `superview` of the `UIButton`. Plus, you can then avoid multiple extra calls to `tableView:cellForRowAtIndexPath:`. – mbm29414 May 04 '12 at 16:55
  • @mbm30075 not sure how to implement a subclass in the tableView:cellForRowAtIndexPaht: UIButton...I guess my current way was what I was thought...I will try to google your suggestion. thx. – AhabLives May 04 '12 at 17:01
  • I'm guessing that superview is not your tableViewCell. Try: `NSLog(@"%@", [cell class]);` and see what you are getting back. – lnafziger May 04 '12 at 17:09
  • @Inafziger No, actually if I NSLog the *cell it returns the correct cell row data ---2012-05-04 13:12:25.734 AllRe[6595:207] cell row> 2012-05-04 13:12:25.738 AllRe[6595:207] The row id is 0 – AhabLives May 04 '12 at 17:15

2 Answers2

1

See my answer here for a much simpler way of finding the index path of anything tapped in a cell, using the location of the tapped item, and indexPathForRowAtPoint.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • returned null. It has has something to do with calling "self.tableView" ??? Not sure how or why but I think I am losing the data there. NSLog of *cell in buttonPressed returns correct data. – AhabLives May 04 '12 at 17:34
  • Do you have an outlet defined for the tableView, and have you connected it to the tableView? This is done automatically in a table viewcontroller but I see you're using a plain UIViewController. – jrturton May 04 '12 at 18:40
0

I would implement a UIButton subclass. Let's call it ALAccessoryButton.

// Button code
@interface ALAccessoryButton : UIButton
@property (strong, nonatomic) NSIndexPath *indexPath;
@end

@implementation ALAccessoryButton
@synthesize indexPath;
@end

// In your UITableView 
- (void)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)p {
    ALAccessoryButton *btn = [ALAccessoryButton buttonWithType:UIButtonTypeContactAdd];
    [btn addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [btn setIndexPath:p];
    [cell setAccessoryView:btn];
}

- (void)checkButtonTapped:(ALAccessoryButton *)sender {
    NSIndexPath *path = [sender indexPath];
    // Now, do whatever else you need to do...
}

Make sense? This answer assumes that you main problem (and the root of your question) is getting the location/index path of the cell for the tapped button, right?

mbm29414
  • 11,558
  • 6
  • 56
  • 87