【发布时间】: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