UITableViewCell, two cells are both getting an accessory view - ios

Situation: When a user selects a cell, a button is added to that cell. When they select a new cell, the button is removed from the previous cell and added to the new cell. That works. The problem is when more data is added to the table. So lets say there are 20 cells, then I add another 20 cells. I then select the first cell, however the button is added to cell 1 AND cell 21. The select delegate method only registers the first one being selected though.
From my didSelectRowAtIndexPath method:
if (self.selectedCell) {
self.selectedCell.accessoryView = nil;
self.selectedCell = nil;
}
UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadButton setTitle:#"Download" forState:UIControlStateNormal];
[downloadButton setFrame:CGRectMake(0, 0, 130, 34)];
self.selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
self.selectedCell.accessoryView = downloadButton;
[self.selectedCell setNeedsDisplay];
In my method that adds more data to the table I end with:
if(self.selectedCell){
self.selectedCell.accessoryView = nil;
self.selectedCell = nil;
}
[self.tableView reloadData];
[self.tableView setNeedsLayout];

Cells are reused. In your cellForRowAtIndexPath:, you're forgetting to clear accessoryView. When the cell is reused, the accessoryView comes along.
A technique I like is to set the accessoryView in tableView:willDisplayCell:forRowAtIndexPath:. This is called right before the cell is put on the screen. You can then do something like:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.isSelected) {
cell.accessoryView = self.downloadButton; // No reason to create a new one every time, right?
}
else {
cell.accessoryView = nil;
}
}

Related

Messing UIButton in each UICollectionViewCell in Scrolling Filmstrip

This is a follow-up question to my previous one and this time I have a problem with UIButton that I have added in each UICollectionViewCell. Before diving into the problem, let me brief out all I've got so far.
Here's the basic rundown of how my UICollectionView in Scrolling Filmstrip style within UITableView works:
Create a normal UITableView with a custom UITableViewCell
Create a custom UIView that will be added to the cell's contentView
The custom UIView will contain a UICollectionView
The custom UIView will be the datasource and delegate for the
UICollectionView and manage the flow layout of the UICollectionView
Use a custom UICollectionViewCell to handle the collection view data
Use NSNotification to notify the master controller's UITableView when
a collection view cell has been selected and load the detail view.
So far, I've been able to add UIButton in each UICollectionViewCell and when I tap the UIButton its image will change to checked mark but the problem occurs when I scroll up/down. Once the cell with checked mark UIButton is off-screen and scrolled back on-screen again, UIButton image starts to get messed around. For example, when UIButton image in cell1 should be checked mark but it's not. Instead checked mark will be appeared in another cell.
Here below is my relevant code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PostsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"PostsCollectionViewCell" forIndexPath:indexPath];
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
cell.postTextLabel.text = [cellData objectForKey:#"text"];
cell.locationNameLabel.text = [cellData objectForKey:#"locationName"];
// >>> Select Button <<<
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setFrame:CGRectMake(110, 150, 20, 20)];
[selectButton setImage:[UIImage imageNamed:#"uncheckedDot.png"] forState:UIControlStateNormal];
[selectButton setImage:[UIImage imageNamed:#"checkedDot.png"] forState:UIControlStateSelected];
[cell.contentView addSubview:selectButton];
[selectButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
// >>> End Select Button <<<<
return cell;
}
// >>> Select Button Method <<<
-(void) buttonPressed:(UIButton *)sender
{
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Any help would be greatly appreciated! Thanks in advance.
Man, you should be aware that cells (both for UITableView and UICollectionView) are reused via the reuse deque under the hood of UIKit and are not obligated to keep their state consistent with regard to their position in a table view or a collection view. What you need to do is to make sure that the return value of dequeueReusableCellWithReuseIdentifier: is then properly initialized (or, in your case, re-initialized) in accordance with the state of your data model. Basically, you need to store the state of your "checkmarks" (or "checkboxes", what have you) in an array or any other data structure. Then, when you call dequeueReusableCellWithReuseIdentifier:, you should apply that stored state to the return value (the cell you're going to return for the collection view) you just got. Hope it's explained good enough. Good luck.
Maintain in your Data source which cell is to be selected by adding a key for each data source dict. For example:#"isSelected"
Then in cellForRowAtIndexPath:
if ([[cellData valueForKey:#"isSelected"] boolValue]) {
[selectButton setSelected:NO];
} else {
[selectButton setSelected:YES];
}
and in your -(void) buttonPressed:(UIButton *)sender Method:
CGPoint point = [self.collectionView convertPoint:CGPointZero fromView:sender];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
if ([[cellData valueForKey:#"isSelected"] boolValue]) {
[cellData setValue:#"false" forKey:#"isSelected"];
} else {
[cellData setValue:#"true" forKey:#"isSelected"];
}
[self.framesCollectionView reloadItemsAtIndexPaths:#[indexPath]];
AND
In your cellForRowAtIndexPath set a tag for the UIButton. Then before adding a UIButton as subview to the cell's contentView check if a view with this tag already exists. In case the cell has been reused, just use the already existing button.

UITableView Scroll automatically to the top after begin/end updates call for adjust height

I have a TableViewController that initialize my cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FIDPostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell for this indexPath
[cell updateFonts];
[cell loadDataWithPost:[self.posts objectAtIndex:indexPath.item]];
cell.parentTableViewController = self;
cell.indexPath = indexPath;
[cell draw];
if(self.selectedIndex == indexPath.row){
//Do expand cell stuff
} else{
//DO closed cell stuff
}
return cell;
}
and that responds to heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = CellIdentifier;
FIDPostTableViewCell *cell = [self.offscreenCells objectForKey:reuseIdentifier];
if (!cell) {
cell = [[FIDPostTableViewCell alloc] init];
[self.offscreenCells setObject:cell forKey:reuseIdentifier];
}
// Configure the cell for this indexPath
[cell updateFonts];
[cell loadDataForHeightCalculationWithPost:[self.posts objectAtIndex:indexPath.item]];
[cell draw];
if(self.selectedIndex == indexPath.row){
return [cell calculateHeight] + 100;
} else{
return [cell calculateHeight];
}
}
self.selectedIndex is a int local variable of TableViewController
Each custom cell have inside a button, that respond to a selector when touched, this is my CustomViewCell code:
self.expandSocialAction = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.expandSocialAction.backgroundColor = [FIDUIHelper fideniaLightBlue];
[self.expandSocialAction addTarget:self action:#selector(selectRow:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.expandSocialAction];
and then:
-(void)selectRow:(id)sender{
if(self.parentTableViewController.selectedIndex == self.indexPath.row){
self.parentTableViewController.selectedIndex = -1;
} else{
self.parentTableViewController.selectedIndex = self.indexPath.row;
}
[self.parentTableViewController.tableView beginUpdates];
[self.parentTableViewController.tableView endUpdates];
}
The cell have a pointer to parent tableViewContorller: self.parentTableViewController.
All work fine, after beginUpdate and endUpdates call, the method heightForRowAtIndexPath is called ( i put a break point in ) and also che cell have the right height.
If i click the button on the first or second row the cell animate and change height fine, but if i scroll down the table and for example i click on the 6th row, the height change but the tableView scroll automatically to the first or second element.
Any suggestion?
Regards,
I have a feeling that the heightForRowAtIndexPath: method is part of the problem. Instead of performing the calculation, try to just use two constants in that method: one for expanded, one for collapsed. I'm wondering if the method is taking too long to execute and then thinks the height of the rows are 0, then the calculation finally returns, but the UITableView scroll position is not updated.
Another suggestion might be that your estimatedHeight and heightFor methods are returning two vastly different values.
check if you have used ScrollTORowAtIndexPAth someWhere in your code

Changing the accessory view of cell in a user defined function

I want to change the accessory view of the cell. When the user clicks on the right navigation bar item an action occurs, in that action i want to change the accessory view. how can i do this, when i used this code its asking for NSindexPath thing so i have no which value i can give here , in order to change the view of all cells.
MyCustomCell *cell = (MyCustomCell *) [self.tableView cellForRowAtIndexPath:helloIndexPath];
if( cell.accessoryType == UITableViewCellAccessoryDisclosureIndicator)
{
cell.accessoryView = [[UIImageView alloc]initwithImage:[UIImage imageNamed:#"hello.png"]];
}
I guess you should put condition in table view - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Here you can put a if-condition.
if(flagForAction){
cell.accessoryView = [[UIImageView alloc]initwithImage:[UIImage imageNamed:#"hello.png"]];
}
else
{
cell.accessoryView = [[UIImageView alloc]initwithImage:[UIImage imageNamed:#"<no-Hello>.png"]];
}
All you need to do is set your flag and reload table at action. by calling [<your table> reloadData];

Managing UITableViewCell selected states

I have UITableViewController with a static table view and 2 sections. The first section has two cells, with a layout Table View Cell > Content View > Text Field.
The second section of table view cells has a layout that is Table View Cell > Content View > Label - and the cells expand when they're selected by using beginUpdates and endUpdates and reporting cell heights via tableView:heightForRowAtIndexPath:.
Right now, when I touch one of the first sections' cells, the keyboard comes up and the cursor is placed in the text field, but the cell is not selected - which means that if I had previously selected one of the cells in the second section, it would remain selected (and therefore expanded).
Also, if I have selected a cell in the first section with the text field given first responder, then I select a cell in the second section, the cell expands, but the first cell does not resign first responder and dismiss the keyboard.
Did I layout my table view incorrectly such that the cell selected states are not managed automatically and if not: what is the proper way to manage the selected states of the UITableViewCells?
I dont know how you have written the code. But from above information I have created one sample application. The code is as below and it is working as you want.
I have considered the table with 2 section.
First section has the textfield on Cell.
Second section has the UIlabel on the cell.
txtFieldCopy is of type UITextField declared in .h of class.
Please check the way I have assigned the Tag to the view as we need it to get the indexpath.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellStyle style =UITableViewCellStyleSubtitle;
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
int iTag=[indexPath row]+1;
txtFieldCopy=nil;
if(indexPath.section==1)
{
UILabel *lblFirst=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier];
lblFirst = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 100,
40)] ;
lblFirst.tag=iTag;
[cell.contentView addSubview:lblFirst];
lblFirst=nil;
}
lblFirst = (UILabel *)[cell.contentView viewWithTag:iTag];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
lblFirst.text =#"Label 1";
}
else
{
UITextField *txtFirst=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier];
txtFirst = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 50,
40)] ;
txtFirst.tag=iTag;
txtFirst.delegate=(id)self;
[cell.contentView addSubview:txtFirst];
txtFirst=nil;
}
txtFirst = (UITextField *)[cell.contentView viewWithTag:iTag];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
txtFirst.text =#"text 1";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(txtFieldCopy)
{
[txtFieldCopy resignFirstResponder];
txtFieldCopy=nil;
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%d",textField.tag);
NSIndexPath *iRowId =[NSIndexPath indexPathForRow:(textField.tag-1) inSection:0];
[tblSample selectRowAtIndexPath:iRowId animated:NO scrollPosition:UITableViewScrollPositionMiddle];
txtFieldCopy=textField;
}
Make sure if suppose in between you are reloading the table view then txtFieldCopy=nil; in the cellForRowAtIndexPath get called and set to null. so you can ignore this line. But before please check whether you are calling it again and again.
Let me know if it worked for you or not. I am sure it definitely work for you

Losing UIButton within a custom cell when reloading table

I've custom UITableViewCells where I placed buttons dynamically in code. I have programmatically set actions for such buttons, and when the action is triggered, then I need to reload the table, among other things. Once reload is called, the sender button disappears, and I don't know why... Could somebody give me a hint for the possible reason? If button is not tapped, and I scroll up and down through the table, it remains there.
Thanks!
EDIT
This is a code snippet from cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *listData =[self.tableViewEntries objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0: {
cell = [self.tableView dequeueReusableCellWithIdentifier:#"firstCell"];
((CustomCell *)cell).cellTextField.tag = firstCellTag;
cell.tag = firstCellTag;
break;
}
case 1: {
cell = [self.tableView dequeueReusableCellWithIdentifier:#"secondCell"];
((CustomCell *)cell).cellTextField.tag = secondCellTag;
cell.tag = secondCellTag;
break;
}
}
return cell;
}
The CustomCell I've defined is loaded from a nib. Those cells have a UITextField for user input and a UIView where I set or remove the buttons dynamically in code according to the user input within the text field. The buttons are created in code, and their actions are set in code as well:
- (void)setButtonForCell:(UITableViewCell *)cell withResult:(BOOL)isValid
{
UIImage* validationButtonNormalImage = nil;
UIImage* validationButtonHighlightImage = nil;
UIButton *validationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[validationButton setFrame:CGRectMake(0, 0, ((CustomCell *)cell).cellValidationView.frame.size.width, ((CustomCell *)cell).cellValidationView.frame.size.height)];
if (isValid) {
validationButtonNormalImage =[[UIImage imageNamed:#"success.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
validationButtonHighlightImage =[[UIImage imageNamed:#"success.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
[validationButton removeTarget:self
action:#selector(validationButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
else {
validationButtonNormalImage =[[UIImage imageNamed:#"error.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
validationButtonHighlightImage =[[UIImage imageNamed:#"error.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
[validationButton setTag:cell.tag];
[validationButton removeTarget:self
action:#selector(validationButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[validationButton addTarget:self
action:#selector(validationButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
[validationButton setBackgroundImage:validationButtonNormalImage forState:UIControlStateNormal];
[validationButton setBackgroundImage:validationButtonHighlightImage forState:UIControlStateHighlighted];
[((CustomCell *)cell).cellValidationView.subviews makeObjectsPerformSelector: #selector(removeFromSuperview)];
[((CustomCell *)cell).cellValidationView addSubview:validationButton];
}
Then, in validationButtonTapped:, I do some tasks and call to [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:NO];, and there is when I loose the button from the view. However, if I don`t tap the button and I scroll down and up again to get 'cellForRowAtIndexPath:' called again, the button remains here. It is like I only loose it when calling for a reload.
Any help?
Cells are usually reused if you are following the usual cellForRowAtIndexPath: pattern. Do you remove all subviews in prepareForReuse for each cell? If you didn't then you would see unexpected buttons on cells as you scroll up and down. You probably need to add the buttons as required in each cell's configuration after you get the cell for reuse (or in newer code, dequeue a cell) in cellForRowAtIndexPath:.

Resources