2012-05-04 71 views
0

具有索引0 I有一個UITableViewUIViewController內部。數據,字幕數據和附件按鈕均可正常加載;但是,當我按下任何附件按鈕,他們都返回索引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頁。

+0

在'tableView:cellForRowAtIndexPath:'中設置按鈕本身的值不是更容易嗎?如果必須,你可以繼承UIButton並添加一個自定義的'NSIndexPath'屬性。看起來比去UIButton的'superview'更容易也更可靠。另外,你可以避免多次調用'tableView:cellForRowAtIndexPath:'。 – mbm29414

+0

@ mbm30075不知道如何在tableView中實現一個子類:cellForRowAtIndexPaht:UIButton ...我想我目前的方式是我的想法...我會嘗試谷歌你的建議。謝謝。 – AhabLives

+0

我猜superview不是你的tableViewCell。試試:'NSLog(@「%@」,[cell class]);'看看你回來了什麼。 – lnafziger

回答

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... 
} 

有意義嗎?這個答案假設你的主要問題(和你的問題的根源)是獲取點擊按鈕的單元格的位置/索引路徑,對嗎?

1

查看我的回答here可以更簡單地找到點擊單元格中任何東西的索引路徑,使用點擊條目的位置和indexPathForRowAtPoint。

+0

返回null。它與調用「self.tableView」有關?不知道如何或爲什麼,但我認爲我在那裏丟失了數據。 buttonPressed中* cell的NSLog返回正確的數據。 – AhabLives

+0

你有沒有爲tableView定義的插座,並且你有連接它到tableView?這是在表視圖控制器中自動完成的,但我看到你正在使用普通的UIViewController。 – jrturton