【问题标题】:Loading Images into a tableview cell, according to object loaded from NSUserDefaults根据从 NSUserDefaults 加载的对象,将图像加载到 tableview 单元格中
【发布时间】:2013-02-16 15:51:23
【问题描述】:

我有一个由从 NSUserDefaults 加载的对象填充的 tableview。

特别 - 当用户在一个视图控制器中按下按钮时,一个字符串被加载到 NSUserDefaults 中,然后被加载到一个数组中,最终填充一个表格视图。

我想知道如何根据从 NSUserDefaults 加载的字符串在每个 tableview 单元格中加载图像?

我不想在 NSUserDefaults 中保存图像,因为我知道这不是一个好主意。

我能做什么?

感谢您抽出宝贵时间(帮助新手)!


2013 年 2 月 19 日更新

我加载到 NSUserDefaults 中的字符串是对象的名称,例如“ARCX”。

目前我已经尝试了各种代码,但我似乎存在误解/缺乏知识(或两者兼而有之)。

UITableViewCell *XCell = [tableView cellForRowAtIndexPath:IndexPath];
NSString *content = Xcell.textLabel.text;

if ([content isEqualToString:@"ARCX"]) [UIImage *Image = [UIImage imageNamed @"ARCX.png"]];

显然我从这段代码中得到了错误。

希望你能帮上忙!


2013 年 2 月更新

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *string = [myOBJArray objectAtIndex:indexPath.row];
     static NSString *CellIdentifier = @"XCell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UIView *cellbackground = [[UIView alloc] initWithFrame:CGRectZero];
        cellbackground.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"MSP cell bg MK2.png"]];
        cell. backgroundView = cellbackground;

}

cell.textLabel.textColor = [UIColor yellowColor ];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:24.0];
cell.textLabel.text = string;


 UITableViewCell *XCell = [tableView cellForRowAtIndexPath:indexPath];
 NSString *content = XCell.textLabel.text;

 if ([content isEqualToString:@"ARCX"]) [UIImage *Image = [UIImage imageNamed: @"ARCX.png" ]];


cell.imageView.image = Image;

     return cell;

}

我确实有我最初在 CellForRowAtIndexPath 中发布的代码,但我知道我在实现中遗漏了一些东西(显然因为它不起作用!)。

感谢您的宝贵时间!


2013 年 2 月 19 日更新

I 这是我当前的 CellForRowAtIndexPath。没有错误,但我的单元格中没有图像。我错过了什么?

我必须添加图像视图吗?我目前使用的是“基本”单元,而不是自定义单元。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [myObjArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"MSCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    UIView *cellbackground = [[UIView alloc] initWithFrame:CGRectZero];
    cellbackground.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"MSP cell bg MK2.png"]];
    cell. backgroundView = cellbackground;
}

cell.textLabel.textColor = [UIColor blackColor ];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:24.0];
cell.textLabel.text = string;

if ([string isEqualToString:@"ARCX"])

cell.imageView.image = [UIImage imageNamed: @"ARCX.png"];

else if ([string isEqualToString:@"WPX"])

    cell.imageView.image = [UIImage imageNamed:@"WPX.png"];


 return cell;

}

感谢您的宝贵时间!

【问题讨论】:

  • 你加载到 NSUserDefaults 的字符串是什么。是远程网址吗?请发布一些您尝试过的示例代码。
  • 我已经更新了我的问题,感谢您的宝贵时间!
  • 您应该在 tableview 的 cellForRowAtIndexPath 委托方法中添加您的 UIImage。请在该方法中发布代码。
  • 我已经发布了 CellForRowAtIndex 的代码,希望对您有所帮助!

标签: image uitableview xcode4.5 nsuserdefaults


【解决方案1】:
UITableViewCell *XCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *content = XCell.textLabel.text;

请删除这段代码,因为它会一次又一次地调用 cellForRowAtIndexPath。 您已经将图像名称作为字符串

NSString *string = [myOBJArray objectAtIndex:indexPath.row];

所以修改如下代码。

if ([string isEqualToString:@"ARCX"]) 
        UIImage *Image = [UIImage imageNamed: @"ARCX.png"];
cell.imageView.image = Image;

【讨论】:

  • 亲爱的,人力资源管理 - 你的代码看起来很接近,但是当我实现它时抛出了两个错误。我已将其稍微更改为您在我最近的编辑中可以看到的内容。虽然它现在没有错误,但它仍然不起作用!你能帮忙吗?
  • 请检查 2 件事:1)在您的 ifelse if 条件中添加 NSLog 并确保已设置 cell.imageView。2)尝试在您的中添加 UIImageView使用您提供的图像名称查看并验证图像是否正确显示。
  • 尊敬的 HRM,我检查了 cell.imageView,这很好,但这样做我的 Mac 崩溃了。重新启动时,我清理了项目,现在它可以工作了!真奇怪,谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
相关资源
最近更新 更多