2017-02-09 89 views
0

我需要知道如何通过滑动动画向左或向右时,实现这种定制UITableViewCell的,就像UITableViewCell的出的现成功能。添加一个单选按钮的UITableViewCell通过动画:iOS的

我到目前为止已经完成(和工作),是:

  1. 添加一个按钮与圆形图像。
  2. 切换该按钮的可见性。
  3. 切换按钮的突出显示和正常状态。
  4. 当在#2中显示或隐藏单选按钮时,切换整个视图的约束。
  5. 重新加载数据。

我在想,这个问题可能会使我的实现更清洁,而不是切换我的自定义tableViewCell的可见性和约束,只需将其滑动或动画化即可。

编辑:这些单选按钮只出现在某种模式下,如编辑模式。通常情况下,我的单元格中没有单选按钮。

enter image description here

+0

你想让这个按钮只出现在编辑模式下吗? –

+0

对,@LoryHuz OHHHH是的! – Glenn

回答

1

你可以试试这个,我创建简单的演示。

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 

查看Controller.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 

输出看起来就像这样:

在开始不显示复选框:

enter image description here 点击时显示复选框: enter image description here

+0

+1。谢谢你。我会编辑我的问题,使其更清晰。我只想在编辑模式或某种模式下显示这个单选按钮。这意味着,通常没有单选按钮。抱歉。 – Glenn

+0

这是对我来说'最'正确的答案。我已经实现了这一点。不过,显然,这是一个私人API。但我,我已经这样做了。如果该应用被拒绝,然后重新实施我的最后解决方案或@Lory Huz's。 – Glenn

1

让我们考虑您的数据源看起来像

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]]; 

和你的表委托/数据源的方法应该是

- (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]; 
} 
1

你做的难,你只需要隐藏自己的按钮,这里是一个类似的实现我也有一个删除按钮,并删除它没有确认。

1)在制造内容视图子视图,并带一个负空间,所以比内容视图大(-42是滑动的偏移值)

enter image description here

然后把你的勾号按钮在这个隐蔽的空间,像我一样为我:

enter image description here

然后,当你将设置编辑模式为真,你的复选框会出现幻灯片动画;)

+0

这似乎是正确的答案。看起来很有希望给我。 :)本周我会选择一个答案,需要先实施。 – Glenn

+1

不客气,小心约束条件,需要正确设置以获得正确的效果:)但不需要代​​码。 –

相关问题