2013-02-16 122 views
0

我有一个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; 

}

感谢您的时间!

+0

什么是您加载到NSUserDefaults的字符串。它是一个远程网址吗?请张贴您尝试过的一些示例代码。 – HRM 2013-02-18 12:05:30

+0

我已更新我的问题,谢谢你的时间! – 2013-02-19 14:09:07

+0

您应该添加你的'UIImage'在tableview中的'cellForRowAtIndexPath'委托方法。请以该方法发布代码。 – HRM 2013-02-20 03:53:41

回答

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; 
+0

亲爱的,人力资源管理 - 你的代码看起来非常接近,但是当我实现它时抛出了两个错误。我已将其稍微更改为可在最近的编辑中看到的内容。尽管它现在没有错误,但它仍然不起作用!你能帮我吗? – 2013-02-21 13:04:28

+0

请检查两件事情:1)添加'NSLog'在'if'和'其他if'条件,并确保'cell.imageView'是set.2)只是试图添加一个'UIImageView'在视图与图像并且验证图像是否正确显示。 – HRM 2013-02-21 14:23:34

+0

亲爱的人力资源管理局,我检查了cell.imageView,那很好,但是这样做让我的Mac崩溃。重启后,我清理了该项目,现在它可以正常工作!多么奇怪,谢谢你的帮助! – 2013-02-21 16:07:44

相关问题