【问题标题】:Remove checkmark删除复选标记
【发布时间】:2016-12-04 08:48:55
【问题描述】:

我有一个表格视图。如果我单击单元格,它将开始下载带有UIActivityIndicator 动画的文件。下载完成后出现复选标记(文件存在),用户可以移动到下一个控制器。必要的是,在移动到下一个控制器并返回后,所有复选标记都消失了。怎么做?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [NSString stringWithFormat:@"Cell%d", indexPath.row] forIndexPath:indexPath];
if (indexPath.row == 1){
if (!fileExists) {
        [_spinner startAnimating];
    }
    if (fileExists) {
 cell.accessoryView = nil;
 cell.accessoryType = UITableViewCellAccessoryCheckmark;
 }
    }

    if (indexPath.row == 2){
if (!fileExists1) {
        [_spinner1 startAnimating];
     }

        if (fileExists1) {
            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1) {

if (!fileExists) {
 _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
 _spinner.frame = CGRectMake(0, 0, 24, 24);
 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 cell.accessoryView = _spinner;
 tableView cellForRowAtIndexPath:indexPath].accessoryView = _spinner;
 [_spinner startAnimating];

if (fileExists) {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

}
}
}
}

【问题讨论】:

  • 您能否添加更多代码,例如cellForRowAtindexPathdidSelectRowAtIndexPath
  • @user 检查答案,这是您的要求吗?
  • @NiravD 我更新了我的问题。请检查。
  • 在不更新 model(数据源数组)的情况下,基本上不要操作 view(单元格)。下次重新加载表视图时,只考虑模型中的当前状态。
  • 希望对您有所帮助:stackoverflow.com/questions/20045991/…

标签: ios objective-c cell


【解决方案1】:

在 viewWillAppear 中重新加载你的 tableview

【讨论】:

  • -(void)viewWillAppear:(BOOL)animated{ [self.tableView reloadData]; }我添加了代码,但它不起作用
  • 再次检查你的tableview对象,连接是否给定。
【解决方案2】:

检查结果:

按照我的代码:

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

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

@property (nonatomic, strong) NSIndexPath *sel_indexPath;  // selected indexpath

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    // cancel the checkmark

    if (self.sel_indexPath) {

        [self.tableView reloadData];
    }

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return 5;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellId"];

    }

    cell.accessoryType = UITableViewCellAccessoryNone;

    cell.textLabel.text = @"cell title";

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

    self.sel_indexPath = indexPath;
    [self performSegueWithIdentifier:@"VC1GotoVC2" sender:self];
}

@end

【讨论】:

  • 我更新了我的问题。请检查。如果我在cellForRowAtIndexPath 中使用UITableViewCellAccessoryNone,它是行不通的。复选标记外观
  • @user 你的意思是selectrow 现在没有推送到下一个控制器吗?
【解决方案3】:

如果文件存在,则在 viewWillAppear 中重新加载您的 tableView。使用模型标记 fileExists 单元格。

@interface CheckMarkModel ()

@property (assign, nonatomic,get=isFileExists) BOOL fileExists;

@end

@implementation CheckMarkModel

@end

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSIndexPath selectIndexPath;  // select IndexPath

@property (strong, nonatomic) NSMutableArray *dataArrM; // save select cell 
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    // your cell count
    NSInteger cellCount = ;
    _dataArrM = [NSMutableArray array];
    for (int i = 0; i < cellCount; ++i)
    {   
        CheckMarkModel *model = [CheckMarkModel new];
        [_dataArrM addObject:model];
    }
    // reload
    [tableView reload];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
    //....
    CheckMarkModel *model = _dataArrM[indexPath.row];
    cell.accessoryType = (model.isFileExists) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

    return  cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!self.isFileExists)
    {
        _selectIndexPath = indexPath;
        [self downloadFile];
        [self tableView:tableView didDeselectRowAtIndexPath:indexPath];
    }else {
        // move to the next controller 
    }

}
- (void)downloadFile {
    // download action ...

    //download success
    if (fileExists) {
        CheckMarkModel *model = _dataArrM[_selectIndexPath.row];
        model.fileExists = YES; 
        [tableView reloadRowsAtIndexPaths:@[_selectIndexPath] withRowAnimation:UITableViewRowAnimationNone];     
    }   
}
@end

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2016-08-19
    相关资源
    最近更新 更多