未选择单元格时,从UITableViewCell删除辅助视图,并在两次重载视图之间进行维护 [英] Removing an Accessory View from a UITableViewCell When The Cell is Not Selected and maintaining between reloading views

查看:48
本文介绍了未选择单元格时,从UITableViewCell删除辅助视图,并在两次重载视图之间进行维护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,但找不到解决方案.

I have what I believe to be a simple problem, but I can't find a solution to it.

我有一个带有两个静态 cells (亮和暗)的静态 UITableView .

I have a static UITableView with two static cells (Light and Dark).

当用户选择一个单元格时,我正在显示带有自定义图像的附件视图.当用户选择另一个单元格时,我希望第一个单元格取消选择并删除自定义的AccessoriesView,并将其提供给secondCell.我希望它能够继续根据用户选择的单元格来执行此操作.

When the user selects one of the cells, I'm displaying an accessoryView with a custom image. When the user selects the other cell, I want the first cell to deselect and to remove the custom accessoryView and for that to be provided to the secondCell. I want this to continuously do this depending on which cell the user selects.

同样重要的是,我需要确保在整个此视图控制器的重新加载过程中(使用 NSUserDefaults ),选定的"单元格都维护了AccessoriesView.

Of equal importance, I need to make sure the "selected" cell maintains the accessoryView throughout the reloading of this view controller (with the use of NSUserDefaults).

我将如何去做?我是iOS开发的新手.

How would I go about doing this? I'm fairly new to iOS development.

在我的didSelectRow中有一些代码:

Here's some code in my didSelectRow:

 BOOL isSelected;

if (cell.isSelected) {
    UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(165, 10, 14, 15)];
    dot.image=[UIImage imageNamed:@"check-white-hi.png"];
    [cell addSubview:dot];
    cell.accessoryView = dot;
    isSelected = YES;
}
else {
    cell.accessoryView = nil;
    isSelected = NO;
}

通过从一个单元格和另一个单元格删除附件视图,以及如何在应用程序的整个生命周期中维护该视图,将非常感谢您的协助.

Any assistance would really be appreciated with removing the accessory view from one of the cells and to the other, and also how to maintain this throughout the lifecycle of the app.

谢谢

推荐答案

在.h文件中创建属性

@property (nonatomic,retain)NSIndexPath * checkedIndexPath ;

在您的.m文件中合成

@synthesize checkedIndexPath;

在您的didSelectRowAtIndexPath方法中编写此代码

write this code in your didSelectRowAtIndexPath method

if(self.checkedIndexPath)
{
   UITableViewCell* uncheckCell = [tableView
                                    cellForRowAtIndexPath:self.checkedIndexPath];
    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
    self.checkedIndexPath = nil;
}
else
{
   UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}

这篇关于未选择单元格时,从UITableViewCell删除辅助视图,并在两次重载视图之间进行维护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆