I have a table view and some buttons inside it. 4 buttons in one row then another 4 in second row and so one. I am using array for adding images to the buttons.
For the first time it is working properly but when delete all the items in the array and new objects to the array and then call reload table view, the new data is not reloaded.
What could be the reason?
I am posting a part of my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// }
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button1 setImage:[array objectAtindex:0]
[button1 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:self.button1];
button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button2 setImage:[array objectAtindex:1] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
}
else if(indexPath.row == 1)
{
button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button3 setImage:[array objectAtindex:2] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button4 setImage:[array objectAtindex:3] forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button4];
}
else
{
// Do something here
}
return cell;
}
my click button:
-(IBAction)clickToDisplayImage {
[text resignFirstResponder];
if([imageArray count] >0)
{
[imageArray removeAllObjects];
}
int totalImages = [text.text intValue];
for( int i=0;i<totalImages;i++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]];
}
[tableView reloadData];
}
When you call reloadData on your UITableView, your cellForRowAtIndexPath message will be called to populate the row. Because you have already created the cells from the first pass, when you call dequeueReusableCellWithIdentifier you will get back your originally created cells associated with the @”Cell” identifier.
You can confirm this by setting a breakpoint and checking that the second time through you will get your cell from dequeueReusableCellWithIdentifier (rather than a newly created cell). You will then be adding new subviews to that original cell (so four button subviews for the cell, then six, eight etc…).
What you should be doing is only adding the button subviews when you first create the cell (so move the button create stuff into the if (cell == nil) block) and you should be using a cell identifier based on the table row. Something like:
N.B. I haven’t run this code through a compiler, so ware syntax errors 😉