簡體   English   中英

在表格視圖的每一行中添加多個按鈕,但不起作用

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

大家好,我是IOS開發的初學者。 我創建了一個表格視圖,每行有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

您應該以編程方式為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{
}

如果某些方法無效或您需要更多信息,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM