我有一个tableview由从NSUserDefaults加载的对象填充。根据从NSUserDefaults加载的对象将图像加载到tableview单元格中
具体 - 当用户按下在一个视图控制器的按钮,一个字符串被加载到NSUserDefaults的,然后将其加载到一个数组,最终填充的表图。
我想知道我怎么能在各tableview中细胞加载图像,依赖于从加载NSUserDefaults的字符串是什么?
我不想保存图像NSUserDefaults的,因为我知道这是不是一个好主意。
我能做些什么?
谢谢你的时间(帮助新手出去)!
更新19FEB13
我加载到的NSUserDefaults的字符串是对象的名称,例如 “ARCX”。
目前,我已经尝试了各种代码段的,但我的地步,我似乎有一种误解/缺乏知识(或两者)。
UITableViewCell *XCell = [tableView cellForRowAtIndexPath:IndexPath];
NSString *content = Xcell.textLabel.text;
if ([content isEqualToString:@"ARCX"]) [UIImage *Image = [UIImage imageNamed @"ARCX.png"]];
很明显,我从这段代码中得到错误。
我希望你能帮忙!
更新20FEB13
- (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的代码,但我知道我失去了一些东西在执行(很明显,因为它不工作!)。
谢谢你的时间!
更新19Feb13
我这是我目前的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的字符串。它是一个远程网址吗?请张贴您尝试过的一些示例代码。 – HRM 2013-02-18 12:05:30
我已更新我的问题,谢谢你的时间! – 2013-02-19 14:09:07
您应该添加你的'UIImage'在tableview中的'cellForRowAtIndexPath'委托方法。请以该方法发布代码。 – HRM 2013-02-20 03:53:41