简体   繁体   English

在表格视图的每一行中添加多个按钮,但不起作用

[英]Add multiple buttons in each row of tableview but doesn't work

Hi guys I'm a beginner in IOS development. 大家好,我是IOS开发的初学者。 I have created a tableview with 3 buttons in each row. 我创建了一个表格视图,每行有3个按钮。 And I have coded a method for one of the buttons. 而且我已经为其中一个按钮编写了一种方法。 It turns out the button only works in the last row. 原来,该按钮仅在最后一行有效。 Please help me with this. 请帮我解决一下这个。

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

Only in the last row, the button works. 该按钮仅在最后一行有效。 I tested the tag for each button. 我测试了每个按钮的标签。 It's valid; 是有效的;

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

You should create buttons for each row in a tableView programmatically or create custom cell via storyboard. 您应该以编程方式为tableView中的每一行创建按钮,或者通过情节提要创建自定义单元格。

First way: 第一种方式:

-(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];
......
}

Then create 3 function: 然后创建3个函数:

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

If something doesn't work or if you need more information let me know. 如果某些方法无效或您需要更多信息,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM