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