在tableview的每一行中添加多个按钮但不起作用

时间:2014-02-09 08:07:02

标签: ios button tableview

大家好我是IOS开发的初学者。我创建了一个tableview,每行有3个按钮。我已经为其中一个按钮编写了一个方法。事实证明,该按钮仅适用于最后一行。请帮帮我。

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

1 个答案:

答案 0 :(得分: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{
}

如果某些内容不起作用或者您需要更多信息,请与我们联系。