2014-02-09 14 views
0

嗨,大家好,我是IOS開發的初學者。我已經創建了每行3個按鈕的tableview。我已經編寫了其中一個按鈕的方法。原來這個按鈕只能在最後一行工作。請幫我解決一下這個。在tableview的每一行添加多個按鈕,但不起作用

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
UITableViewCell *cell; 
NSInteger row = [indexPath row]; 
...... 
startButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
pauseButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
stopButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
[pauseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[stopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[startButton setBackgroundImage:[UIImage imageNamed:@"tianlanse.png"] forState:UIControlStateNormal]; 
[pauseButton setBackgroundImage:[UIImage imageNamed:@"lianglv.png"] forState:UIControlStateNormal]; 
[stopButton setBackgroundImage:[UIImage imageNamed:@"qianhong.png"] forState:UIControlStateNormal]; 

startButton.frame = CGRectMake(522.0f, 26.0f, 65.0f, 50.0f); 
pauseButton.frame = CGRectMake(443.0f, 26.0f, 65.0f, 50.0f); 
stopButton.frame = CGRectMake(603.0f, 26.0f, 65.0f, 50.0f); 

stopButton.hidden = YES; 
pauseButton.hidden = YES; 

startButton.tag = indexPath.row+1001; 
pauseButton.tag = indexPath.row+2002; 
stopButton.tag = indexPath.row+3003; 
[startButton setTitle:@"Start" forState:UIControlStateNormal]; 
[cell addSubview:startButton]; 


[pauseButton setTitle:@"Pause" forState:UIControlStateNormal]; 
[cell addSubview:pauseButton]; 

[stopButton setTitle:@"Stop" forState:UIControlStateNormal]; 
[cell addSubview:stopButton]; 
} 
[startButton addTarget:self action:@selector(StartbuttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
...... 
} 

-(IBAction)StartbuttonClicked:(id)sender{ 
NSUInteger row = ((UIButton *)sender).tag-1001; 
NSLog(@"start button clicked,Row:%lu",(unsigned long)row); 

[stopButton viewWithTag:((UIButton *)sender).tag+2002].hidden = NO; 
[pauseButton viewWithTag:((UIButton *)sender).tag+1001].hidden = NO; 
[startButton viewWithTag:((UIButton *)sender).tag].hidden = YES; 
} 

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{ 
return tableCellCount;//tableCellCount is set to be 4 in viewdidload 
} 

只有在最後一行,按鈕才起作用。我測試了每個按鈕的標籤。這是有效的;

2014-02-09 15:38:31.919 Weight Training[336:60b] 1001,2002,3003 
2014-02-09 15:38:31.921 Weight Training[336:60b] 1002,2003,3004 
2014-02-09 15:38:31.923 Weight Training[336:60b] 1003,2004,3005 
2014-02-09 15:38:31.925 Weight Training[336:60b] 1004,2005,3006 

回答

0

您應該以編程方式爲tableView中的每一行創建按鈕或通過故事板創建自定義單元格。

第一種方式:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
UITableViewCell *cell; 
NSInteger row = [indexPath row]; 
...... 
UIButton *startButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
[startButton addTarget:self action:@selector(startButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
[pauseButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
[stopButton addTarget:self action:@selector(stopButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

[pauseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[stopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[startButton setBackgroundImage:[UIImage imageNamed:@"tianlanse.png"] forState:UIControlStateNormal]; 
[pauseButton setBackgroundImage:[UIImage imageNamed:@"lianglv.png"] forState:UIControlStateNormal]; 
[stopButton setBackgroundImage:[UIImage imageNamed:@"qianhong.png"] forState:UIControlStateNormal]; 

startButton.frame = CGRectMake(522.0f, 26.0f, 65.0f, 50.0f); 
pauseButton.frame = CGRectMake(443.0f, 26.0f, 65.0f, 50.0f); 
stopButton.frame = CGRectMake(603.0f, 26.0f, 65.0f, 50.0f); 

stopButton.hidden = YES; 
pauseButton.hidden = YES; 

startButton.tag = indexPath.row+1001; 
pauseButton.tag = indexPath.row+2002; 
stopButton.tag = indexPath.row+3003; 

[startButton addTarget:self action:@selector(StartbuttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
...... 
} 

然後建立3個功能:

- (void) startButtonClicked:(id) sender{ 
} 
- (void) stopButtonClicked:(id) sender{ 
} 
- (void) pauseButtonClicked:(id) sender{ 
} 

如果有什麼不工作,或者如果你需要更多的信息,讓我知道。

相關問題