UITableView Cell Button mixed up - iphone

I have a problem, setting a button to a UITableviewCell.
After viewDidLoad, the button is on the right place. But when I am scrolling down, the button is anyplace else.
Here is my code, I hope you can help me.
Thanks In Advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0 && indexPath.row == 0 && _isAddImageViewLoad == NO) {
// Add Image Button
UIButton *addImage = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage* image = [UIImage imageNamed:#"AddImage#2x"];
addImage.frame = CGRectMake(110.0f, 10.0f, 110.0f, 110.0f);
[addImage setImage:image forState:UIControlStateNormal];
[cell.contentView addSubview:addImage];
_isAddImageViewLoad = YES;
} else {
NSDictionary *dictionary = [_items objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"data"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
return cell;
}

It is because you are reusing the cells, and the button is getting placed when it shouldn't an easy solution in your else section. Write addImage.hidden = YES; and in your if statement put addImage.hidden = NO;

Just a couple things. If you use "AddImage" it will use the "AddImage#2x" automatically if it's a retina display. I don't think that will solve your issue but it could be causing weirdness.
When a table view cell is scrolled off the view it is "recycled" in a sense. It appears like you are using a bool to exclude the original cell from being loaded again with a button. You may want to use a header to hold your button if you always want it at the "top". You may also want to verify that the button is being removed when the cell is reused. if its not it will show up in the next row that reuses that cell.
On a side note... Buttons don't usually work very well in table view cells because they handle touches in very different ways. It's quite a bit of modification to get them to feel natural but that's another matter.
Hope that helps!

The problem is because of cell reuse. You need to put some code in the else clause to delete the button if it exits. One way to do this, would be to give your button a tag, like:
addImage.tag = 10;
Then in your else clause:
}else{
if (cell viewWithTag:10) [[cell viewWithTag: 10] removeFromSuperview];
...

The problem is because of the dequeue for the cells. The first time the tableview creates the cells, all the cells run through the
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
code. But when the section 0 row 0 is moved off the screen, that cell is pushed into the cell reusable queue.
Now when your tableview needs to display section 0 row 0, it will get a cell from the reuse queue. you will not get the same cell as the first time. So now you might have 2 cells with the button.
What you should do is have different CellIdentifier for section 0 row 0 , and all other sections and rows. Also create the button when creating the cell. So after the first time the tableView creates the cell, you will not be creating the the button everything.

Look at this line of code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This line of code means the UITableViewCells are not created every time. They are re-used as you scroll up and down. Using the code you have above, the UIButton will be created in the correct spot, but then as the cells are re-used, it will create the button in random spots.
One quick way to solve the problem, change the above line of code to simply
UITableViewCell *cell;

Related

search results tableview not displaying the contents of its cells even while filtering of the contents of the table is working fine

i have a viewcontroller containing multiple tableviews managed by the same viewcontroller.i added search to one of the tableviews... i have added the searchbar amd searchisplaycontroller and wired up the outlets for searchdelegate,searchdatasource,searchbar.etc...
i have the code for filtering the contents of the table working correctly...
below is the code inside the cellforrowatindexpath method
static NSString *simpleTableIdentifier = #"search";
SearchFieldsCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[SearchFieldsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *field=nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
field = _searchfieldsArray[indexPath.row];
}
else{
field = _fieldsArray[indexPath.row];
}
cell.titleLabel.text=field[#"a"];
cell.subtitleLabel.text=field[#"b"];
cell.detailLabel.text=field[#"c"];
cell.flagImage.image=[UIImage imageNamed:#"ic_flag.png"];
return cell;
but its not displaying the contents of the tableview... even the height of the tableviewcell is set and during the runtime... if i give printobject like po cell.titlelabel.text in the debug console it gives result like
<UILabel: 0xc15d500; frame = (70 0; 241 21); text = 'result'; userInteractionEnabled = NO; layer = <CALayer: 0xc15d5b0>>
i further found out that the line of code
SearchFieldsCell *cell=[tableViewdequeueReusableCellWithIdentifier:simpleTableIdentifier];
is perfectly working ie loading the prototype cells from the storyboard but the line
if (cell == nil) {
cell = [[SearchFieldsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
is returning empty white cells somehow its not loading from the prototype cells from the storyboard... how can this happen...?? anyone please help...
Try to type cast cell with custom cell class. So it will initialize & not be null value.
SearchFieldsCell *cell = (SearchFieldsCell *)[tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];
Hopefully, it will help you.
Thanks.
i know its not done this way...but after so many different ways the solution was to create a custom xib file and make the uisearchresultscontrollers tableview to register this nib file .it worked
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:#"SearchFieldsCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:#"Search"];

What is wrong with my UITableView cellForRowAtIndex for Single Selection?

Below is code for UITableView, But when i scroll its behaves weirdly (too annoying)... This problem is due to reuseIdentifier.... but dont know how to solve..
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
NSInteger imgTag = 1;
NSInteger lblTag = 2;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 52, 52)];
// Image:[UIImage imageNamed:[self.glassType objectAtIndex:indexPath.row]]];
imgView.tag = imgTag;
[cell.contentView addSubview:imgView];
[imgView release];
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(60, cell.frame.size.height/4, 200, 21)];
// lblName.text = [self.glassName objectAtIndex:indexPath.row];
lblName.tag = lblTag;
[cell addSubview:lblName];
[lblName release];
}
NSInteger imgIndex = 2;
NSInteger lblIndex = 3;
((UIImageView *)[cell viewWithTag:imgTag]).image = [[self.glassType objectAtIndex:indexPath.row] objectAtIndex:imgIndex];
((UILabel *)[cell viewWithTag:lblTag]).text = [[self.glassName objectAtIndex:indexPath.row] objectAtIndex:lblIndex];
return cell;
}
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView1 cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
How to make Cell for row at index so that it remains constant even when scrolled??? Also how to make single selection in UITableView??
The answer is that you should not add subviews to your table cells outside of the "if (cell == nil) { ..." clause or they get added over and over again to the same cell when it gets re-used.
See my answer to this question for a more detailed explanation, including code for how to fix it:
cellForRowAtIndexPath memory management
You also cannot store state in table cells because as soon as they scroll offscreen they are recycled and re-appear at a different index in your table. You need to set up an array of model objects to store state for your table cells (such as what their accessory type should be). A more detailed explanation can be found in my answer to this question:
Looping through UITableViewCells of a UITableView
If you fix how you are adding subviews to the cells, and store your "ticked" state in an array of model objects as well as setting the cell.accessoryType (so that it can be restored when the cell is dequeued), then your approach to row selection is otherwise correct.
So put this in your tableView:cellForRowAtIndexPath: method, just before the return cell;:
MyModelObject *object = [self.arrayOfModelObjects objectAtIndex:indexPath.row];
BOOL isChecked = object.checked;
cell.accessoryType = isChecked? UITableViewCellAccessoryCheckmark: UITableViewCellAccessoryNone;
And in your tableView: didSelectRowAtIndexPath: method, get rid of the current logic and replace it with:
- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
for (int i = 0; i < [self.arrayOfModelObjects count]; i++)
{
MyModelObject *object = [self.arrayOfModelObjects objectAtIndex:i];
object.checked = (i == indexPath.row); // only check the one we just tapped
}
//refresh table to update the accessory views for all rows
[tableView1 reloadData];
}
Obviously replace the arrayOfModelObjects with your own model implementation. You could just use an array of NSNumber objects containing bools if you don't want to create a custom class for this purpose.
The recycling queue is like a pool where previously created Cells are stored before to reuse them. For example when you scrolls up, at the moment the cell disappears above, it is stored in the queue and becomes available for the cell that will appear at the bottom. Ok ?
Actually the number of cells really created is exactly the max simultaneous cell you can display in your table (in most cases from 3 to 8). In other words your if (cell == nil) code is executed (more or less from 3 to 8 times) at the first reloadData to create the pool of cells your table needs.
Then all you make on a cell is kept as it and appears again when you dequeue it. It's now easy to understand that, in your code, you have to make all strictly row-dependant settings outside the if (cell == nil) block. The same way, do not add subViews outside the if (cell == nil) block, you can imagine the thousands of subview you will add each time you reset a dequeued cell !
Tip: if you need some custom cleanup before reusing a cell (like to set an image to blank), you can create a custom UITableviewCell class and implements the prepareForReuse method.
Is it clear ?
Always reload your tableView in viewWillAppear method instead of viewDidLoad.
(void) viewWillAppear:(BOOL)animated{
[self.tableView reloadData];
}
This avoids most of all unexpected and annoying problems. :)

data in the table row , appears and disappears when we scroll up (or) scroll down the page

I have created a grouped table view like in the image below. but data in the table row , appears and disappears when we scroll up (or) scroll down the page , when we scroll up first 2 rows will go up (beyond the view) then if we scroll down both rows will take same value,, y its so? can any one help me
Thanks in advance.
like following code i am designing the table cell where CustomCellStudentData is class there i am creating like lable frames, text field frames
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCellStudentData *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(100,10, 200, 40)];
cell.accessoryView = tfText[0];
tfText[0].delegate=self;
tfText[0].placeholder =#"<Student Name>";
cell.primaryLabel.text =#"Student Name: ";
}
break;
case 1:
{
tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(100,10, 200, 40)];
cell.accessoryView = tfText[1];
tfText[1].delegate=self;
tfText[1].placeholder =#"<Student Age>";
cell.primaryLabel.text =#"Student Age: ";
}
}
}
UITableView reuses the cells so as to not bog down the memory. As soon as the two cells go up, they enter the pool of reusable cells. If the pool is not empty, dequeueReusableCellWithIdentifier: returns a cell from that pool (there is a dependency on the identifier here). As the table view needs cells for the new rows being scrolled in, it gets the cells from the pool and provides it to you for reconfiguration. So, the text field that you attached to the cell when configuring it for row 0 and 1 still remain unless you explicitly remove. Since we don't which cells we are going to get, we should reset all customizations that we do and configure it as a new cell. In this case, irrespective of the cell coming in, you should remove the text field as the accessory view first.
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
What Deepak said is correct.
Adding to his answer, in future, if you add any subView to cell view. Remove from the cell before you add anything on it. Use
for(UIView * view in cell.contentView.subviews){
[view removeFromSuperview]; view = nil;
}
before customizing the cell.
And yes, please increase your acceptance rate. Going back to the questions you asked before and accepting some answers should help.

Custom drawing in UITableView failing after 5 cells

I'm having a funky problem with drawing a custom view in a UITableView. The drawRect function of my custom view, called ChatBubbleView, is just drawing a rectangle of a constant size.
I have an NSLog debugging statement in the drawRect function of ChatBubbleView. The first 5 times I add a cell to the UITableView, everything draws nicely, and I see the NSLog statement trigger. After 5 however, the drawing becomes garbled, and I no longer see the debugging statement.
I'm totally baffled, and I'd appreciate any help you might provide. Thanks in advance!
Here is the function that is called when reloadData is called on the UITableView.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (forumMode) {
//This part works, ignore
} else {
ChatBubbleView* chatBubble = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
// Add the bubble
chatBubble = [[[ChatBubbleView alloc] init] autorelease];
chatBubble.tag = BUBBLEVIEW_TAG;
[cell.contentView addSubview:chatBubble];
} else {
// Reuse old views
chatBubble = (ChatBubbleView*)[cell.contentView viewWithTag: BUBBLEVIEW_TAG];
}
chatBubble.labelSize = CGSizeMake(50, 18);
chatBubble.frame = CGRectMake(0, 0, chatBubble.labelSize.width, chatBubble.labelSize.height);
}
return cell;
}
EDIT: I should add that the function that reloads the UITableView looks like this:
-(IBAction) btnAdd:(id) sender {
[listOfMessages addObject:textView.text];
[self.tableView reloadData];
}
EDIT 2: I've discovered that the custom views in the cells sometimes redraw themselves when I scroll the cells in and out of view. It seems that the tableview refreshing changes the parameters of the custom view.
You say //This part works, ignore but if you are reusing the CellIdentifier it might not work as the cell that will get dequeued could be a different type of cell (that happens to have the same CellIdentifier). Make sure each cell type has a unique CellIdentifier.
To be sure, put some debugging in your if / else block to see what cell you're getting back...

What is a good way to animate UIViews inside UITableViewCell after a callback?

We couldn't find a way to animate UIViews inside a UITableCell as an action from a callback.
Suppose in a scenario where you click on a button on the UITableViewCell and it fires off an asynchronous action to download a picture. Suppose further that when the picture is downloaded we want a UIView in the cell to animate the picture to give user a visual feedback that something new is about to be presented.
We couldn't find a way to track down the UIVIew to invoke beginAnimation on because the original cell that the user clicked on might now be used for another row due to the nature of cells being reused when you scroll up and down in the table. In other words we can't keep a pointer to that UITableViewCell. We need to find another way to target the cell and animate it if that row is visible and don't animate if the row is scrolled out of range.
Keep the cell object different from the object being animated so the cell holds a UIView. When the animation callback occurs check to make sure that the UIView still exists and, if it does, animate the changes.
When the cell object gets bumped off the screen and recycled, release the UIView that would have been animated and create a new one. When the animation callback occurs it will have nothing to do because the UIView no longer exists.
A modification of the above is to keep some sort of object in the UIView that your callback can check to see if the animation is still appropriate. This could be some sort of unique identifier for the picture being downloaded. If the identifier changes, no animation is needed. If it matches, do the animation.
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier] autorelease];
} else {
UIView *oldViewToAnimate = [cell.contentView viewWithTag:1];
[oldViewToAnimate removeFromSuperview];
}
UIView *viewToAnimate = [[UIView alloc] initWithFrame:CGRectZero]; //replace with appropriate frame
viewToAnimate.tag = 1;
[cell.contentView addSubview:viewToAnimate];
return cell;
}
When you spawn your download process you pass in [cell.contentView viewWithTag:1]. When the download is done, it will update the appropriate view. If the table cell was reused the view will no longer have a superview and will not update the wrong cell.
There are things you can do to make this more efficient but this is the basic idea. If you have a custom UITableViewCell than this will probably look a bit different.
EDIT 2:
To reuse the viewToAnimate objects to make sure that they get updated if their parent cells were recycled, do something like the following:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier] autorelease];
} else {
UIView *oldViewToAnimate = [cell.contentView viewWithTag:1];
[oldViewToAnimate removeFromSuperview];
}
UIView *viewToAnimate = [self viewToAnimateForIndexPath:indexPath];
viewToAnimate.tag = 1;
[cell.contentView addSubview:viewToAnimate];
return cell;
}
viewToAnimateForIndexPath will need to:
Check to see if a viewToAnimate has been created for this indexPath
Create a viewToAnimate if there isn't one
Save a reference to the view that can be looked up by indexPath
Return the viewToAnimate so the table cell can use it
I don't know enough about your data structure to do this for you. Once the download process completes it can call this same method to get the view and animate it.