Question

I am currently creating a portion of my iPhone app to basically have a list of cells (in a tableview) That act like the existing apple notepad.

I am trying to make it so the names of the cells have the names of strings in an array. This is what I am currently doing.

@interface ViewController ()
{
NSMutableArray *cameraArray;
NSMutableArray *notesArray;
NSMutableArray *voiceArray;
}
@end

@implementation ViewController
//@synthesize myTableView;
- (void)viewDidLoad
{
[super viewDidLoad];

NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
//[ud setObject:@"Archer" forKey:@"char1class"];
[ud synchronize];
NSString *key1;//[ud stringForKey:@"Key1"];
NSString *key2; //[ud stringForKey:@"Key1"];
NSString *key3; //[ud stringForKey:@"Key1"];

if([ud stringForKey:@"Key1"] == nil){
    key1 = @"Open Camera Slot";
}else{
    key1 = [ud stringForKey:@"key1"];
}

if([ud stringForKey:@"Key2"] == nil){
    key2 = @"Open Camera Slot";
}else{
    key2 = [ud stringForKey:@"key2"];
}

if([ud stringForKey:@"Key3"] == nil){
    key3 = @"Open Camera Slot";
}else{
    key3 = [ud stringForKey:@"key3"];
}

cameraArray = [[NSMutableArray alloc]initWithObjects:key1, key2, key3, nil];


}

//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return cameraArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath{


CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];


if(cell == nil){
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:@"Cell"];
}

NSEnumerator *enumerator = [cameraArray objectEnumerator];
id anObject;
NSString *cellName = nil;
while (anObject = [enumerator nextObject]) {
   cellName = anObject;
}
//static NSString *cellName = [cameraArray.objectAtIndex];
cell.textLabel.text = [NSString stringWithFormat:cellName];
return cell;

}

So I am basically creating the strings in cameraArray from keys in the NSUserDefaults (im just doing this for testing purposes, the strings will be user entered later on)

What I'm stuck on is the enumerator goes through the array just fine, but only uses the last value (the third one) on all of the cells in the tableView.

So if there were three strings, "first" "second" and "third" All three of the cells say "third"

How do I fix this?

Was it helpful?

Solution

There are several things wrong with this code. 'rdelmar' posted the basic answer in his comment. I thought I'd walk a few things as a learning exercise.

You are using an enumerator here to walk through the values of an array. There is a much simpler way. Note that this isn't needed for your code. I am pointing this out for future reference.

Replace the enumerator, anObject, cellName, and the while-loop with this:

for (NSString *cellName in cameraArray) {
    // Do something with cellName
}

This is a much easier way to walk through all the values of an array of NSString objects. If the array contains objects of a different type then replace NSString with the appropriate type.

Next is your use of creating a new string combined with using stringWithFormat:. Neither is needed in such a case. In the code you posted, cellName is already an NSString reference. Just assign it directly such as:

cell.textLabel.text = cellName;

No need to create a new NSString object. And you should only use string formats when you actually have a string format.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top