【问题标题】:How to Add more button to UITableViewCell When scrolling it?滚动时如何向 UITableViewCell 添加更多按钮?
【发布时间】:2014-02-18 08:00:42
【问题描述】:

当我们滚动(向左滑动)UITableViewCell时,它显示一个删除按钮,但我想在上面添加其他按钮,我该怎么办?

我想要的风格就像iOS 7中的系统邮件应用,UITableviewCell中有两个按钮,一个是删除按钮,另一个是更多按钮。

请提出任何想法

谢谢

【问题讨论】:

    标签: ios button uitableview ios7


    【解决方案1】:

    您可以使用此示例方法创建更多按钮

    https://github.com/scheinem/MSCMoreOptionTableViewCell

    此链接示例更有帮助,您可以创建自定义更多按钮。

    https://github.com/CEWendel/SWTableViewCell

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
        static NSString *cellIdentifier = @"Cell"; 
    
        SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    
        if (cell == nil) { 
            NSMutableArray *leftUtilityButtons = [NSMutableArray new]; 
            NSMutableArray *rightUtilityButtons = [NSMutableArray new]; 
    
            [leftUtilityButtons addUtilityButtonWithColor: 
                            [UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]  
                            icon:[UIImage imageNamed:@"check.png"]]; 
            [leftUtilityButtons addUtilityButtonWithColor: 
                            [UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0]  
                            icon:[UIImage imageNamed:@"clock.png"]]; 
            [leftUtilityButtons addUtilityButtonWithColor: 
                            [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0]  
                            icon:[UIImage imageNamed:@"cross.png"]]; 
            [leftUtilityButtons addUtilityButtonWithColor: 
                            [UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0]  
                            icon:[UIImage imageNamed:@"list.png"]]; 
    
            [rightUtilityButtons addUtilityButtonWithColor: 
                            [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] 
                            title:@"More"]; 
            [rightUtilityButtons addUtilityButtonWithColor: 
                            [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]  
                                title:@"Delete"]; 
    
            cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
                            reuseIdentifier:cellIdentifier  
                            containingTableView:_tableView // For row height and selection 
                            leftUtilityButtons:leftUtilityButtons  
                            rightUtilityButtons:rightUtilityButtons]; 
            cell.delegate = self; 
        } 
    
        NSDate *dateObject = _testArray[indexPath.row]; 
        cell.textLabel.text = [dateObject description]; 
        cell.detailTextLabel.text = @"Some detail text"; 
    
        return cell; 
    } 
    

    【讨论】:

    • MSCMoreOptionTableViewCell 使用方便,解决了我的问题。非常感谢!
    【解决方案2】:

    here 所述,您可以使用额外的 UITableView 数据源/委托方法实现与 Mail.app 中相同的单元格布局

    - (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
    - (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    【讨论】:

      【解决方案3】:

      有可能,请按以下步骤操作:

      1) 委托函数canEditRowAtIndexPath 应该返回NO

      2) 使用cellForRowAtIndexPath 在 UITableView 中加载自定义单元格。

      3) 在自定义单元格中,显示两个或更多按钮并使用以下方法处理可见性:

      [btn setHidden:NO];
      

      希望它能给你一个想法,如何开始。

      【讨论】:

        【解决方案4】:

        这真的很简单,不要重新发明轮子使用这个控件(自定义 UITableViewCell)它是开源的并且工作得很好。

        如果您不只是想将其放入并使用它,那么至少您可以准确了解他们是如何做到的! https://www.cocoacontrols.com/controls/swtableviewcell

        【讨论】:

          【解决方案5】:

          Apple 已经为此添加了自己的支持

          - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
              UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
          
              }];
              editAction.backgroundColor = [UIColor blueColor];
          
              UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete" handler^(UITableViewRowAction *action, NSIndexPath *indexPath) {
          
              }];
          
              return @[deleteAction, editAction];
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-06
            • 2019-05-12
            • 2013-08-31
            • 2016-04-30
            • 1970-01-01
            相关资源
            最近更新 更多