PengOne2
PengOne2

Reputation: 31

UiTableView bug, added cell doesnt insert text needed

Hey guys i have a table view that can add and delete cells. but when i delete the only cell that comes with the app, and i try to add a new cell with the text of "Meghan Way" and the text just automatically changes it self to the original cells text which is "House 1" here is my code!

- (IBAction)saveButton:(id)sender {
FacePlatesViewController * viewController = (FacePlatesViewController   
*)self.parentViewController;

[viewController addObject:[NSMutableDictionary dictionaryWithObject:text.text    
forKey:@"name"]]; 

[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   
(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero   
reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:@"name"];
UIImage * myimage = [UIImage imageNamed: @"check.png"];
image = [[UIImageView alloc]initWithImage:myimage];



tableView.backgroundColor = [UIColor clearColor];


    self.myTableView = tableView;


}
return cell;

}

this is for the save button! any help would be very appreciated:D Thanks

Upvotes: 0

Views: 317

Answers (2)

Akshay
Akshay

Reputation: 5765

You code is quite messed up. I have improved it below and added comments:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   
(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero   
    reuseIdentifier:CellIdentifier] autorelease];
    }  //The if block should end here. You should set the cell's label irrespective whether the cell was nil. This is the cause of the issue you are facing.

    cell.textLabel.text = [[cells objectAtIndex:indexPath.row] valueForKey:@"name"];

    //You are doing nothing with the UIImage
    //UIImage * myimage = [UIImage imageNamed: @"check.png"];
    //image = [[UIImageView alloc]initWithImage:myimage];


    //You should be setting the background color just once, in ViewDidLoad, not here.
    //tableView.backgroundColor = [UIColor clearColor];

    //I don't see why this is required
    //self.myTableView = tableView;

    return cell;

}

Upvotes: 1

Delete
Delete

Reputation: 922

Are you storing the text labels for these cells in any kind of array or dictionary and loading them in there using the standard "cellForRowAtIndexPath?" If so, then you need to delete the dictionary / array entry in that data source to prevent it from being used again.

I can't tell for sure without seeing some more code...but it sounds like a cell recycling issue. I could be wrong...but I'd need to see more info.

Upvotes: 0

Related Questions