首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell自定义编辑内容

UITableViewCell自定义编辑内容
EN

Stack Overflow用户
提问于 2014-10-10 12:30:48
回答 4查看 3.7K关注 0票数 2

我希望我的单元格不仅在编辑时有删除按钮,而且还有其他的。那么,实际的方法是什么呢?我尝试为UIView属性创建自己的editingAccessoryView对象,但问题是该视图只出现在直接编辑(即单击导航栏中的edit按钮),而完全忽略了在单元格上滑动。同时,tableView:editActionsForRowAtIndexPath:方法也尝试了一次,但没有成功,至少我没有想出如何用它的函数得到我想要的东西。提前谢谢你!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-10-10 12:36:57

你试试这个密码

代码语言:javascript
复制
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
    {
        NSLog(@"Action to perform with Button 1");
    }];
    button.backgroundColor = [UIColor greenColor]; //arbitrary color
    UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        NSLog(@"Action to perform with Button2!");
                                    }];
    button2.backgroundColor = [UIColor blueColor]; //arbitrary color

    return @[button, button2]; //array with all the buttons you want. 1,2,3, etc...
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:

}
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES; //tableview must be editable or nothing will work...
    }
票数 4
EN

Stack Overflow用户

发布于 2014-10-10 12:58:47

你也是CanTry这个代码

代码语言:javascript
复制
.H File
{
    NSMutableArray *dataArray;

}


@property (weak, nonatomic) IBOutlet UITableView *atableView;

@property(nonatomic,strong)  NSMutableArray *dataArray;

**`.M file`**


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dataArray = [[NSMutableArray alloc] initWithObjects:@"Saurabh Sharma",@"Deepesh Jain",@"Ashish Sharma",@"Chandan",@"kanhaiya",@"Suchitra Bohra",@"Neha",@"Ghanshyam",nil];
    self.title = @"Move Rows";


    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(editButton:)];

    [self.navigationItem setRightBarButtonItem:editButton];



    // Do any additional setup after loading the view from its nib.
}
    -(void)editButton:(UIBarButtonItem *)button
    {
        {
            if(self.editing)
            {
                [super setEditing:NO animated:NO];
                [atableView setEditing:NO animated:NO];
                [atableView reloadData];
                [self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
                [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
            }
            else
            {
                [super setEditing:YES animated:YES];
                [atableView setEditing:YES animated:YES];
                [atableView reloadData];
                [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
                [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];

    }

        }

    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [dataArray count];
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *cellIdentifair=@"cellIdentifair";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifair];

        if(cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifair];
        }


         cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;

    }

    #pragma delete row in table view
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
        return 10; // This is the minimum inter item spacing, can be more
    }
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellEditingStyleDelete;
    }

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return TRUE;
    }

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView beginUpdates];
        [dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [tableView endUpdates];
        NSLog(@"Methods called when row is deleted");
    }

    #pragma Move Table View Delegte


    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
        NSString *item = [self.dataArray objectAtIndex:fromIndexPath.row];
        [self.dataArray removeObject:item];
        [self.dataArray insertObject:item atIndex:toIndexPath.row];
    }
票数 1
EN

Stack Overflow用户

发布于 2014-11-06 03:35:48

也在斯威夫特工作。

代码语言:javascript
复制
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
        var completionButton = UITableViewRowAction(style: .Normal,
                                                    title: "Complete",
                                                    handler: {(action, index) in
                                                      /* Completion Code */
                                                      println("Complete")})
        completionButton.backgroundColor = .lightGrayColor()
        return [completionButton]
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26299491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档