【问题标题】:How to detect multiple buttons in tableview cell如何检测表格视图单元格中的多个按钮
【发布时间】:2015-07-09 22:39:18
【问题描述】:

如何检测 tableview 单元格中的多个按钮,我的疑问是例如我在单元格中有 3 个按钮,如果我点击一个按钮,该按钮将改变颜色,如果我单击 indexpath.row=1 单元格按钮,该按钮将着色也需要换帮我

【问题讨论】:

  • "indexpath.row=1 cell button" ,你想说什么?
  • 如果我单击按钮需要更改的任何按钮,则每个单元格中只有 3 个按钮
  • 您必须将按钮硬编码到具有唯一标签的表格单元格中,并为每个按钮分配一个带有参数的操作(按钮标签,Indexpath.row)。
  • 下面写的代码在按钮按下时改变颜色是正确的。但是您的问题是要重用单元格。当您滚动表格单元格重用时,它将使用您在 cellForRowAtIndexPath 中设置的默认颜色进行初始化,并且您之前选择的颜色消失了。 :D

标签: ios objective-c tableviewcell


【解决方案1】:

使用节和行的组合为每个按钮分配标签..

当分配给按钮的方法被调用时,使用 '%' 和 '/' 进行进一步操作。

如果你在实现它有任何困难,请告诉我。

【讨论】:

    【解决方案2】:

    转到您的 CustomCell.h 并在 #import 正下方添加此代码。

    @protocol CustomCellDelegate <NSObject>
    - (void)buttonActionwith :(NSIndexPath *)indexPath;
    @end
    

    然后在@interface CustomCell 正下方添加此代码:UITableViewCell

    //Manual Properties
    @property (strong, nonatomic) NSIndexPath *buttonIndexPath;
    
    //Delegate
    @property (nonatomic, weak) id <CustomCellDelegate> delegate;
    

    这个 NSIndexPath 的用途是识别用户选择了哪个单元格。

    然后转到 CustomCell.m 并在您的按钮 IBAction 方法中添加此代码。

    [self.delegate buttonActionwith:self.buttonIndexPath];
    

    这行代码的意思是,当用户TouchUpInside Button在cell buttonActionwith委托方法调用时,如果你在UITableView所在的UIViewController中设置了这个CustomCellDelegate,那个委托方法也会在那个ViewController里面调用。

    现在转到您的 MainViewController.h 并添加 自定义CellDelegate & 之后会是这个样子,

    @interface MainViewController : UIViewController <CustomCellDelegate,,UITableViewDataSource,UITableViewDelegate>
    

    在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath创建自定义UITableViewCell时别忘了在return cell前加上这行代码;

    //Set Cell Values Here
    cell.delegate = self; //Setting delegate to self
    cell.buttonIndexPath = indexPath;
    

    修改后的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath会是这个样子,

    static NSString *MyIdentifier = @"MyIdentifier"; //Set Identifier for cell
    
            CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; //init CustomCell
    
            if (cell == nil) { //Check cell is nill or not
                NSArray *nib;
                nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
                                                    owner:self options:nil]; //if cell is nil add CustomCell Xib
    
                for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
                    cell = (CustomCell *)oneObject;
            }
    
            //Set Cell Values Here
            cell.buttonIndexPath = indexPath
    
            return cell;
    

    最后在 MainViewController.m 中添加这个方法

    #pragma mark - SwipeableCellDelegate
    - (void)buttonActionwith:(NSIndexPath *)indexPath { //Delegate method
        NSLog(@"Button Clicks at index %ld",(long)indexPath.row);
    }
    

    这是在单元格内添加单个按钮的方法。您可以使用此方法为单元格添加更多按钮。

    【讨论】:

    • 这是一个更好的解决方案。
    【解决方案3】:

    我是这样做的:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *MyIdentifier = @"MyIdentifier";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier];
        }
    
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
        button.tag = 100 + indexPath.row*total_buttons_in_a_row;
        [button setTitle:[NSString stringWithFormat:@"%ld",(long)button.tag] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button];
    
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button2.frame = CGRectMake(10.0+self.tableView.frame.size.width/4+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
        button2.tag = 100 + indexPath.row*total_buttons_in_a_row + 1;
        [button2 setTitle:[NSString stringWithFormat:@"%ld",(long)button2.tag] forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button2];
    
        UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button3.frame = CGRectMake(10.0+self.tableView.frame.size.width/4*2+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
        button3.tag = 100 + indexPath.row*total_buttons_in_a_row + 2;
        [button3 setTitle:[NSString stringWithFormat:@"%ld",(long)button3.tag] forState:UIControlStateNormal];
        [button3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button3];
    
        return cell;
    }
    
    -(void)btnClicked:(UIButton *)sender{
        id selectedButton = [self.view viewWithTag:sender.tag];
        if ([selectedButton backgroundColor] == [UIColor redColor]) {
            [selectedButton setBackgroundColor:[UIColor clearColor]];
    
        }else{
            [selectedButton setBackgroundColor:[UIColor redColor]];
        }
    }
    

    total_buttons_in_a_row 是一个Int。在你的情况下定义它 viewDidLoad total_buttons_in_a_row=3

    P.S - 根据您的需要设置按钮CGRectMake。

    【讨论】:

    猜你喜欢
    • 2018-08-04
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多