繁体   English   中英

如何在UITableview中存储UIButton图像

[英]How to store an UIButton image in UITableview

我是IOS的新手,我有一个UITableview,其中有三个按钮,当我选择按钮图像时,它将改变。 我的问题是:当我退出应用程序并再次返回到应用程序时,如何将图像存储在表格视图中,这意味着选定的图像应该在那里。 我怎么做这个过程可以帮助我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell = [table dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(80, 27, 36, 36);
    [button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
    [button1 setImage:[UIImage imageNamed:@"lblue.png"]
             forState:UIControlStateSelected];
    [button1 addTarget:self action:@selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside];

    button1.tag=1;
    [cell.contentView addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(160, 27, 36, 36);
    [button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e"ofType:@"png"]] forState:UIControlStateNormal];
    [button2 setImage:[UIImage imageNamed:@"eblue.png"]
             forState:UIControlStateSelected];

    [button2 addTarget:self action:@selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button2];
    button2.tag=3;


    button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(240, 27, 36, 36);
    [button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
    [button3 setImage:[UIImage imageNamed:@"vblue.png"]
             forState:UIControlStateSelected];

    [button3 addTarget:self action:@selector(radiobtn4:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button3];
    button3.tag=2;

- (void)radiobtn4:(UIButton *)sender

{
    UITableViewCell *cell=(UITableViewCell *)sender.superview;
    if(sender.selected == NO)
    {

        if(sender.tag==1){

            UIButton *otherButton=(UIButton *)[cell viewWithTag:1];
            otherButton.selected=YES;
            UIButton *other=(UIButton *)[cell viewWithTag:2];
            other.selected=NO;

        }else if(sender.tag==2){
            UIButton *otherButton=(UIButton *)[cell viewWithTag:2];
            otherButton.selected=YES;
            UIButton *other=(UIButton *)[cell viewWithTag:1];
            other.selected=NO;


        } else if(sender.tag == 3)
        {
            UIButton *otherButton=(UIButton *)[cell viewWithTag:3];
            otherButton.selected=YES;

        }

    } else  if(sender.selected == YES)

    {
        if(sender.tag==1){
            UIButton *otherButton=(UIButton *)[cell viewWithTag:1];
            [otherButton setImage:[UIImage imageNamed:@"l.png"]
                         forState:UIControlStateNormal];
            otherButton.selected=NO;


        }else if(sender.tag==2){
            UIButton *otherButton=(UIButton *)[cell viewWithTag:2];
            [otherButton setImage:[UIImage imageNamed:@"v.png"]
                         forState:UIControlStateNormal];
            otherButton.selected=NO;

        }  else if(sender.tag == 3)
        {
            UIButton *otherButton=(UIButton *)[cell viewWithTag:3];
            [otherButton setImage:[UIImage imageNamed:@"e.png"]
                         forState:UIControlStateNormal];
            otherButton.selected=NO;

        }

    }

}

以上是我的编码。

使用NSUserDefaults

储存:

[[NSUserDefaults standardUserDefaults] setObject:imageName forKey:@"selected_image"];

正在检索:

NSString *imageName = [[NSUserDefaults standardUserDefaults] stringForKey:@"selected_image"];

*以后编辑

对于您的情况,应该将图像名称保存在radiobtn4:并在cellForRowAtIndexPath:还原cellForRowAtIndexPath:

例如:

- (void)radiobtn4:(UIButton *)sender{
  ....

    if(sender.tag==1){
       ...
       [[NSUserDefaults standardUserDefaults] setObject:imageName forKey:@"selected_image1"];
    }
 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    [button1 setImage:[UIImage imageNamed:[[NSUserDefaults standardUserDefaults] stringForKey:@"selected_image1"];]
         forState:UIControlStateNormal];
    ...
}

您需要将表中每个条目的选定状态存储到数据模型中。 另一位张贴者建议了几种不同的方法来保存该数据。

如何保存在表格视图中显示的其他信息? 建议您在当前数据中添加一个字段,以告知您选择了哪个按钮的单元格。 最好将每个条目的所有信息都保存在一起,而不是创建一个保存在其他地方的新结构。

然后在您的cellForRowAtIndexPath中,根据该信息选择正确的按钮。

现在,您可能有问题。 如果更改最顶部单元格上的选定按钮,请在表格视图中向下滚动某个方式,然后再滚动回到顶部,顶部按钮可能会失去其选定按钮的状态。 您甚至可能会对滚动到视图中的单元格显示错误的选定按钮状态有问题。 这是因为单元将被回收,并且您必须完全配置单元,否则它可能具有上次使用后的剩余设置。

您将使用以下方式执行此操作,选择其中一种。 如果您仅需要保存按钮状态,而其他虎钳则使用CoreData,则建议使用NSUserDefaults

使用NSUserDefaults

http://ios-blog.co.uk/tutorials/quick-tips/storing-data-with-nsuserdefaults/

使用CoreData

http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started

使用Plist读/写

使用网络服务

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM