首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在滚动UITableView时取消切换UISwitch

在滚动UITableView时取消切换UISwitch
EN

Stack Overflow用户
提问于 2013-01-25 19:48:11
回答 3查看 973关注 0票数 1

我有一个UISwitch作为accessoryView的UITableView。我的问题是,如果我切换其中一个开关,然后滚动,这样开关就看不见了,它就会返回到以前的状态。

请参阅video

有没有人知道这可能是为什么以及如何解决它?

下面是添加switch视图并处理其操作的代码。

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"POICell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];

    //set the cell text to the
    cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
    NSString *toggle = [self.toggleArray objectAtIndex:[indexPath row]];
    //add switch
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //create an instance of the database oject
    DataBase * dataBase = [[DataBase alloc] init];

    //open the database connection
    [dataBase openDB];
    NSString *imageName = [dataBase getPinImageNameFromCatID:[self.catIDs objectAtIndex:[indexPath row]]];

    //get the root file path for the images
    NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/pinImages/%@",documentsDirectory, imageName];

    //add image
    NSURL *imageURL = [NSURL fileURLWithPath:filePath];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageView.image = image;
    NSLog(@"%@",[self.catIDs objectAtIndex:[indexPath row]]);

    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
    cell.accessoryView = switchView;
    [switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];

    if ([toggle isEqualToString: @"OFF"]) {
        [switchView setOn:NO animated:NO];
    }else{
        [switchView setOn:YES animated:NO];
    }
    return cell;
}

- (void) switchChanged:(id)sender {

    //get the switch that it was sent from
    UISwitch *switchInCell = (UISwitch *)sender;
    //get the cell it was sent from
    UITableViewCell * cell = (UITableViewCell *) switchInCell.superview;
    //get the row it was sent from
    NSIndexPath * indexpath = [self.inputTableView indexPathForCell:cell];
    //cast the indexpath to int
    NSInteger variable = indexpath.row;
    //set the filter as off in the user defualts.
    [self.filterDic setValue:switchInCell.on ? @"ON" : @"OFF" forKey:[self.catIDs objectAtIndex:variable]];
    //store the newdic in the user defualts
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    //save the dic to user defaults
    [prefs setObject:self.filterDic forKey:@"pinFilters"];

}

谢谢你的帮助。

EN

Stack Overflow用户

发布于 2013-01-25 19:52:50

也许你的细胞正在被回收。

看看这里被接受的答案:

iphone : uitableview : contents of a cell change on scrolling

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14521208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档