简体   繁体   English

通过动画将单选按钮添加到UITableViewCell:iOS

[英]Add a Radio Button To UITableViewCell By Animating: iOS

I need idea how to implement this kind of custom UITableViewCell by animating, just like the out-of-the-box feature of UITableViewCell when sliding left or right. 我需要知道如何通过动画实现这种自定义UITableViewCell ,就像向左或向右滑动时UITableViewCell的即用型功能一样。

What I've done so far (and is working), is: 到目前为止,我所做的(并且正在工作)是:

  1. Add a button with that circle image. 添加带有该圆圈图像的按钮。
  2. Toggle visibility of that button. 切换该按钮的可见性。
  3. Toggle highlighted and normal state of the button. 切换按钮的突出显示和正常状态。
  4. Toggle constraints of the whole views when showing or hiding the radio button in #2. 在#2中显示或隐藏单选按钮时,切换整个视图的约束。
  5. Reload Data. 重新加载数据。

I'm thinking that this question might make my implementation cleaner when instead of toggling visibility and constraints of my custom tableViewCell , just slide it or animate it. 我认为这个问题可能使我的实现更加tableViewCell ,而不是切换自定义tableViewCell可见性和约束,而对其进行滑动或动画处理。

EDIT : These radio buttons appear only in a certain mode, like editing mode. 编辑 :这些单选按钮仅在特定模式下出现,例如编辑模式。 So normally, there are no radio buttons in my cells. 因此,通常情况下,我的单元格中没有单选按钮。

在此处输入图片说明

You can try this I created Simple Demo. 您可以尝试这个我创建的简单演示。

ViewController.h ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *dataArray;
}

    @property (weak, nonatomic) IBOutlet UITableView *mTableView;
// You can toggle the Selection by Means you can show hide the checkboxes
    - (IBAction)didTapBringCheckBoxBtn:(id)sender;

@end

View controller.m 查看控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    dataArray=[[NSMutableArray alloc]init];
    [dataArray addObject:@"Apple"];
    [dataArray addObject:@"Mango"];
    [dataArray addObject:@"Papaya"];
    [dataArray addObject:@"Guava"];
    [dataArray addObject:@"Pineapple"];
    [dataArray addObject:@"Strawberry"];
    [dataArray addObject:@"Banana"];
    [dataArray addObject:@"Grapes"];
    [dataArray addObject:@"Pomegranate"];
    [dataArray addObject:@"Green Tea"];
    [dataArray addObject:@"Raisin"];

    self.mTableView.delegate=(id)self;
    self.mTableView.dataSource=(id)self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Check Box Button Action

- (IBAction)didTapBringCheckBoxBtn:(id)sender {

     if(self.mTableView.editing==YES)
{
[self.mTableView setEditing:NO animated:YES];
}else{
[self.mTableView setEditing:YES animated:YES];
}

}

#pragma mark - UITableView DataSource Methods

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

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

#pragma mark - UITableView Delegate Methods

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 3;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"user de-selected %@",[dataArray objectAtIndex:indexPath.row]);
}

@end

OutPut Will look Like This : 输出看起来像这样:

At Start Not showing Checkboxes : 在开始时不显示复选框:

在此处输入图片说明 On click Show CheckBox : 在点击显示复选框: 在此处输入图片说明

You did the harder, you just need to hide your button, here is a similar implementation I did to have a delete button and delete it without a confirmation. 您做了更努力的工作,您只需要隐藏按钮,这是我执行过的类似操作,有一个删除按钮并在没有确认的情况下将其删除。

1) Made a subview in content view, and made it bigger than content view with a negative leading space (-42 is the value of the slide offset) 1)在内容视图中创建一个子视图,并使其比具有负前导空格的内容视图大(-42是幻灯片偏移的值)

在此处输入图片说明

Then put your checkmark button in this hidden space like I did for mine: 然后像我一样,将您的选中标记按钮放在这个隐藏的空间中:

在此处输入图片说明

Then when you will set editing mode to true, your checkbox will appear with slide animation ;) 然后,当您将编辑模式设置为true时,您的复选框将与幻灯片动画一起出现;)

Lets consider your data source looks like 让我们考虑您的数据源看起来像

NSMutableDictionary *sampleRecord = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"isSelected",@"true",@"title",@"title text", nil];
records = [[NSMutableArray alloc] init];
[records addObject:[sampleRecord copy]];
[records addObject:[sampleRecord copy]];
[records addObject:[sampleRecord copy]];

And your table delegate/data source methods should be 并且您的表委托/数据源方法应为

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

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

    //Initialize your cell
    //Read the all views from your cell
    //Now add the click event to your radio button (UIButton)

    [myButton addTarget:self action:@selector(changeRadioState) forControlEvents: UIControlEventTouchUpInside];

    return cell;
}

- (IBAction)changeRadioState:(id)sender
{
    //Detect the row index fro table where is the button present
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonPosition];

    //Now get the resource data for clicked row
    NSMutableDictionary *record = [records objectAtIndex:indexPath.row];

    //Change the selected state images
    if ([[records valueForKey:@"isSelected"] isEqualToString:@"true"]) {
        [sender setImage:[UIImage imageNamed:@"unSelectStateImage"] forState:UIControlStateNormal];
        [record setValue:@"false" forKey:@"isSelected"];

    }
    else{
        [sender setImage:[UIImage imageNamed:@"selectedStateImage"] forState:UIControlStateNormal];
        [record setValue:@"true" forKey:@"isSelected"];
    }

    //Reload entire table
    [tableView reloadData];

    //Or else you can reload the single row by
    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];
}

MGSwipeTableCell is an easy to use UITableViewCell subclass that allows to display swipeable buttons with a variety of transitions. MGSwipeTableCell是易于使用的UITableViewCell子类,它允许显示具有各种转换的可滑动按钮。

https://github.com/MortimerGoro/MGSwipeTableCell https://github.com/MortimerGoro/MGSwipeTableCell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM