【问题标题】:How to store an UIButton image in UITableview如何在 UITableview 中存储 UIButton 图像
【发布时间】:2015-03-13 09:50:54
【问题描述】:

我是 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;

        }

    }

}

以上是我的编码。

【问题讨论】:

  • 使用数据库或 plist 来存储您的选择..
  • 您正在传递/使用的数组/字典中的主标志

标签: ios objective-c iphone uitableview uibutton


【解决方案1】:

使用NSUserDefaults

存储:

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

检索:

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

*稍后编辑

在您的情况下,您应该将图像名称保存在radiobtn4: 并在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];
    ...
}

【讨论】:

  • 我应该添加这个
【解决方案2】:

您需要将表中每个条目的选定状态存储到数据模型中。另一张海报提出了几种不同的方法来保存这些数据。

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

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

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

【讨论】:

    【解决方案3】:

    您将使用以下方式执行此操作,请选择其中一种。 如果您只需要保存按钮状态,我推荐 NSUserDefaults 其他虎钳使用​​ CoreData

    使用 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 读/写

    使用网络服务

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      相关资源
      最近更新 更多