时间:2015-07-28 13:15:34

标签: ios objective-c iphone uitableview

我以编程方式创建了UITableView,而UITableViewUITableView部分中有3行。我有3行UIButton。最初,三行的所有按钮都具有灰色。如果我选择第二行中的按钮,则第二行中的按钮应为红色,其他所有按钮应为灰色,依此类推。任何人都可以建议我。

3 个答案:

答案 0 :(得分:1)

cellForRowAtIndexPath

UIButton *button = [[UIButton alloc]initWithFrame:Your Frame];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = indexPath.section;
[cell.contentView addSubview:button];

然后编写buttonClicked方法

-(void)buttonClicked:(UIButton*)button
{
    UIButton *but1;
    UIButton *but2;
    if (button.tag == 0)
    {
        but1 = (UIButton *)[self.view viewWithTag:1];
        but2 = (UIButton *)[self.view viewWithTag:2];
        [but1 setBackgroundColor:[UIColor grayColor]];
        [but2 setBackgroundColor:[UIColor grayColor]];
        [button setBackgroundColor:[UIColor redColor]];
    }
    if (button.tag == 1)
    {
        but1 = (UIButton *)[self.view viewWithTag:0];
        but2 = (UIButton *)[self.view viewWithTag:2];
        [but1 setBackgroundColor:[UIColor grayColor]];
        [but2 setBackgroundColor:[UIColor grayColor]];
        [button setBackgroundColor:[UIColor redColor]];
    }
    if (button.tag == 2)
    {
        but1 = (UIButton *)[self.view viewWithTag:0];
        but2 = (UIButton *)[self.view viewWithTag:1];
        [but1 setBackgroundColor:[UIColor grayColor]];
        [but2 setBackgroundColor:[UIColor grayColor]];
        [button setBackgroundColor:[UIColor redColor]];
    }
}

希望它有所帮助。

答案 1 :(得分:1)

试试这个:

按照patil的说明创建按钮(查看下面的答案),并尝试给它一个标记。

     button.tag=indexpath.section;

You need to set target-action of each button.Write the following line in the button action method:

   [button setTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];

然后实现someMethod:像这样:

  - (void)someMethod:(UIButton *)sender {
if (sender.tag == 1) {
    // do action for button with tag 1
     //change color here
  [button setBackgroundColor:[UIColor redColor]];

} else if (sender.tag == 2) {
    // do action for button with tag 2
} // ... and so on
}

答案 2 :(得分:0)

我不太喜欢多个if案例,因为当您sections / rows的数量发生变化时,您需要重新编写一些内容。

这是一个更通用的答案,但它可能不像其他人那么容易。它涉及创建一个UIButton子类和一个UITableViewCell子类。

使用属性创建一个UIButton子类,以存储将显示按钮的单元格的NSIndexPath

//
//  IndexPathButton.h
//
//
//  Created by You on 28/07/15.
//
//

#import <UIKit/UIKit.h>

@interface IndexPathButton : UIButton

@property (nonatomic) NSIndexPath *indexPath;

@end

.m文件中不应该有任何实现。

创建一个UITableViewCell子类,其上包含自定义IndexPathButton。如果您在创建自定义UITableViewCell时需要帮助,我建议您在{或searching上提出新问题。

UIViewController与您的UITableView一起导入自定义UIButton

#import "IndexPathButton.h"

还要创建一个类变量来存储selectedIndexPath:

NSIndexPath *selectedButtonIndexPath;

现在我们可以开始更改UITableViewDataSource方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"SomeCell";
    YourCustomCellWithButton *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
    {
        cell = [[YourCustomCellWithButton alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }

    //Default setup of IndexPathButton on YourCustomCellWithButton
    cell.button.backgroundColor = [UIColor grayColor];
    cell.button.indexPath = indexPath; //Here we store the indexPath for later use
    [cell.button addTarget:self action:@selector(cellButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    //Check here if it should be red or not
    if (selectedButtonIndexPath == indexPath)
    {
        cell.button.backgroundColor = [UIColor redColor];
    }

    return cell;
}

按钮目标操作:

-(void)cellButtonPressed:(IndexPathButton*)sender
{
    //Set the selectedIndexPath
    selectedButtonIndexPath = sender.indexPath;
    [yourTableView reloadData];
}