簡體   English   中英

通過動畫將單選按鈕添加到UITableViewCell:iOS

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

我需要知道如何通過動畫實現這種自定義UITableViewCell ,就像向左或向右滑動時UITableViewCell的即用型功能一樣。

到目前為止,我所做的(並且正在工作)是:

  1. 添加帶有該圓圈圖像的按鈕。
  2. 切換該按鈕的可見性。
  3. 切換按鈕的突出顯示和正常狀態。
  4. 在#2中顯示或隱藏單選按鈕時,切換整個視圖的約束。
  5. 重新加載數據。

我認為這個問題可能使我的實現更加tableViewCell ,而不是切換自定義tableViewCell可見性和約束,而對其進行滑動或動畫處理。

編輯 :這些單選按鈕僅在特定模式下出現,例如編輯模式。 因此,通常情況下,我的單元格中沒有單選按鈕。

在此處輸入圖片說明

您可以嘗試這個我創建的簡單演示。

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

查看控制器.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

輸出看起來像這樣:

在開始時不顯示復選框:

在此處輸入圖片說明 在點擊顯示復選框: 在此處輸入圖片說明

您做了更努力的工作,您只需要隱藏按鈕,這是我執行過的類似操作,有一個刪除按鈕並在沒有確認的情況下將其刪除。

1)在內容視圖中創建一個子視圖,並使其比具有負前導空格的內容視圖大(-42是幻燈片偏移的值)

在此處輸入圖片說明

然后像我一樣,將您的選中標記按鈕放在這個隱藏的空間中:

在此處輸入圖片說明

然后,當您將編輯模式設置為true時,您的復選框將與幻燈片動畫一起出現;)

讓我們考慮您的數據源看起來像

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

MGSwipeTableCell是易於使用的UITableViewCell子類,它允許顯示具有各種轉換的可滑動按鈕。

https://github.com/MortimerGoro/MGSwipeTableCell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM