当点击按钮时,包含按钮的表视图单元格似乎总是具有索引0

时间:2012-05-04 16:36:23

标签: objective-c cocoa-touch uitableview

我的UITableView内有UIViewController。数据,字幕数据和配件按钮都很好;但是,当我按任何配件按钮时,他们都返回索引0? dataSourcedelegate已连接到.xib中的文件所有者我缺少什么?

标题文件:

@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

实施,我的cellForRowAtIndexPath:(按钮部分):

@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;

我的buttonPressed方法:

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

所有这些代码都适用于我的其他专用UITableView页。

2 个答案:

答案 0 :(得分:1)

请参阅我的回答here,找到一种更简单的方法,使用挖掘项目的位置和indexPathForRowAtPoint查找在单元格中挖掘的任何内容的索引路径。

答案 1 :(得分:0)

我会实现一个UIButton子类。我们称之为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...
}

有意义吗?这个答案假设您的主要问题(以及问题的根源)是获取点击按钮的单元格的位置/索引路径,对吗?