iOS - 循环访问单元格并检索数据 [英] iOS - Loop through Cells and retrieve data

查看:92
本文介绍了iOS - 循环访问单元格并检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我对iOS开发工具还不熟悉。

Sorry I'm pretty new to iOS dev.

我从拉出的单元格中设置了 UITableView 来自单个XiB笔尖。我在笔尖中创建了一个开/关开关,我试图在 viewWillDisappear 上保存开关的状态,以获取我所拥有的单元格数。 (确切地说是6个细胞)。

I have a UITableView setup from cells being pulled from a single XiB nib. I've created a on/off switch in the nib, and I am trying to save the state of the switch upon viewWillDisappear for the number of cells that I have. (6 cells to be exact).

如何循环遍历所有单元格并保存此信息?

How can I loop through all the cells and save this information?

我在UIViewController中试过这个获取一个单元格的信息:

I tried this in my UIViewController to get the info for one cell:

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    UITableView *tv = (UITableView *)self.view;
    UITableViewCell *tvc = [tv cellForRowAtIndexPath:0];

}

它给出了错误程序接收信号:EXC_BAD_INSTRUCTION 。

it gives me the error "Program received signal: "EXC_BAD_INSTRUCTION".

我怎样才能做到这一点?

How can I accomplish this?

推荐答案

你有将有效的 NSIndexPath 传递给 cellForRowAtIndexPath:。您使用了0,这意味着没有indexPath。

You have to pass a valid NSIndexPath to cellForRowAtIndexPath:. You used 0, which means no indexPath.

您应该使用以下内容:

UITableViewCell *tvc = [tv cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

但是。不要这样做。不要在UITableViewCell中保存状态。

当交换机改变状态时更新你的dataSource。

BUT. Don't do this. Don't save state in the UITableViewCell.
Update your dataSource when a switch changed its state.

如果你已经实现了UITableViewDataSource方法,那么你的tableView重用单元格的权利。这意味着当单元格被重用时,单元格的状态将会消失。

If you have implemented the UITableViewDataSource methods the right why your tableView reuses cells. That means the state of your cells will vanish when the cells are reused.

你的方法可能有效对于6个细胞。但它将失败9个细胞
如果你在屏幕上滚动第一个单元格,它甚至可能会失败。

Your approach might work for 6 cells. But it will fail for 9 cells.
It will probably even fail if you scroll the first cell off screen.

我写了一个快速演示(如果你不使用ARC添加发布他们是必要的)告诉你应该怎么做:

I wrote a quick demo (if you don't use ARC add release where they are necessary) to show you how you should do it instead:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataSource = [NSMutableArray arrayWithCapacity:6];
    for (NSInteger i = 0; i < 6; i++) {
        [self.dataSource addObject:[NSNumber numberWithBool:YES]];
    }
}

- (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];
        UISwitch *aSwitch = [[UISwitch alloc] init];
        [aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = aSwitch;
    }
    UISwitch *aSwitch = (UISwitch *)cell.accessoryView;
    aSwitch.on = [[self.dataSource objectAtIndex:indexPath.row] boolValue];
    /* configure cell */
    return cell;
}

- (IBAction)switchChanged:(UISwitch *)sender 
{
//    UITableViewCell *cell = (UITableViewCell *)[sender superview];
//    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    CGPoint senderOriginInTableView = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderOriginInTableView];
    [self.dataSource replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:sender.on]];
}

您认为不在单元格中存储状态并不复杂: - )

as you see it's not very complicated to not store state in the cells :-)

这篇关于iOS - 循环访问单元格并检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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