Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading Images into a tableview cell, according to object loaded from NSUserDefaults
    primarykey
    data
    text
    <p>I have a tableview populated by objects loaded from NSUserDefaults. </p> <p>Specifically - when a user presses a button in one view controller, a string is loaded into NSUserDefaults, which is then loaded into an array, which finally populates a table view. </p> <p>I am wondering how I can load an image in each tableview cell, dependent on what string is loaded from NSUserDefaults? </p> <p>I don't want to save images IN NSUserDefaults, as I am aware this is not a good idea. </p> <p>What can I do?</p> <p>Thank you for your time (and helping a novice out)! </p> <hr> <p>Update 19FEB13</p> <p>The Strings I am loading into NSUserDefaults are the names of objects, for example "ARCX".</p> <p>At the moment I have tried a variety of pieces of code, but I am at the point where I seem to have a misunderstanding/lack of knowledge (or both). </p> <pre><code>UITableViewCell *XCell = [tableView cellForRowAtIndexPath:IndexPath]; NSString *content = Xcell.textLabel.text; if ([content isEqualToString:@"ARCX"]) [UIImage *Image = [UIImage imageNamed @"ARCX.png"]]; </code></pre> <p>Obviously I'm getting errors from this code. </p> <p>I hope you can help! </p> <hr> <p>Update 20FEB13</p> <pre><code>- (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; </code></pre> <p>}</p> <p>I did have the code I initially posted in the CellForRowAtIndexPath, but I know I am missing something in the implementation (obviously as it doesn't work!).</p> <p>Thanks for your time! </p> <hr> <p>Update 19Feb13</p> <p>I This is my current CellForRowAtIndexPath. No errors, but yet no images in my cells. What am I missing? </p> <p>Have I got to add an image view? I am currently using a 'basic' cell, not a custom one. </p> <pre><code>- (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; </code></pre> <p>}</p> <p>Thanks for your time! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload