0

我的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页面。

4

2 回答 2

1

请参阅我的答案以更简单的方法使用被点击项目的位置和 indexPathForRowAtPoint 来查找单元格中点击的任何内容的索引路径。

于 2012-05-04T17:24:35.760 回答
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...
}

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

于 2012-05-04T17:12:43.600 回答