How to hide custom check mark in table view - ios5

I have a UITableiew that I want to be able to display a custom check mark when the row is selected (tapped), however, if there is a check mark on any of the other rows, it must be hidden. What is the best way to do this? The IF... doesn't seem to work at all, it will not hide and unhide the check mark as I thought it would.
Thanks in advance.
-PaulS.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
home_2_CustomCell *cell = (home_2_CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
//This will hide the selected row...
cell.imageViewDidSelectRow.hidden = YES;
// if (cell.imageViewDidSelectRow.hidden = NO){
// cell.imageViewDidSelectRow.hidden = YES;
// }
}

1) Maintain a tag in class level as an NSIndexPath variable.
2) Whenever a cell is selected make note of the indexPath and reload the table view.
3) In cellForRowAtIndexPath delegate check for this variable and set marks accordingly.
4) This will not be costly if you have the cell with less information.

You can get the each cell of tableview by this code
UITableViewCell *cell=[product_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:clickedTag inSection:0]];
make one for loop.
for(int i=0;i<[tabledata count];i++){
cell.imageViewDidSelectRow.hidden = YES;
}
for each cell except the current row which is selected and you got only one image displayed for the current row.

Related

Modify existing reusable cells with cellForRowAtIndexPath: method

I use tableview with reusable cells. On each cell I have a textField with text, which I can modify. If text is empty, I delete that cell.
Lets say that we had 100 rows and we want to modify row number 1: we tap on it, give an empty string #"", scroll down to position number 50 and tap on this cell.
What now is going is that we detect tap gesture on another cell and I call method textFieldDidEndEditing: to see should I remove this cell from tableview. I use cellForRowAtIndexPath: to get the modified cell.
The problem is that there appear other cells with empty textField. I delete modified cell, but only one. I think that this is a problem with reusable cells.
Can anybody can help me with this problem?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ImageIdentyfier = #"StandardCellWithImageIdentifier";
StandardCellWithImage *cellImage = (StandardCellWithImage *)[tableView dequeueReusableCellWithIdentifier:ImageIdentyfier];
if(cellImage == nil) {
cellImage = [[StandardCellWithImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageIdentyfier];
}
cellImage.nameLabel.delegate = self;
Item *item = [self.mutableFetchResults objectAtIndex:indexPath.row];
cellImage.nameLabel.text = item.itemText;
cellImage.infoLabel.text = item.itemInfo;
cellImage.checkbox.userInteractionEnabled = YES;
cellImage.nameLabel.userInteractionEnabled = NO;
if(item.itemIsChecked.boolValue == YES) {
cellImage.checkbox.tag = indexPath.row;
[cellImage.tapGesture addTarget:self action:#selector(didSelectedImageAtIndexPath:)];
cellImage.checkbox.image = [UIImage imageNamed:#"checkbox-checked.png"];
} else {
cellImage.checkbox.tag = indexPath.row;
[cellImage.tapGesture addTarget:self action:#selector(didSelectedImageAtIndexPath:)];
cellImage.checkbox.image = [UIImage imageNamed:#"open-checkbox.png"];
}
return cellImage;
}
When you scroll from row 1 to row 50, already existing cells are reused - including your cell with empty textField. That is why you see it several times and why your delete routine removed only one instead of all.
Sounds like your cell creation at cellForRowAtIndexPath method needs fixing to make sure empty textfield is not automatically copied to recycled cells. Without seeing any code, this exercise is left to you.
Looked at code, thanx. Could not see any "easy" fix, so proposing that you should avoid the problem. So instead of checking cell taps, maybe you should check list scrolling.
The problem you have exists only because cell, which was being edited, was recycled due user scrolling the list. Therefore remove the problem by a) don't let user to scroll while editing text or b) stop text edit when user starts scrolling.
when your textFieldDidEndEditing is invoked finish , you should check whether the text is "" if it is "" I think you should delete it from the dataSource and then reloadData
your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method should write like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
XXXXXXXCell *cell = [tableView dequeueReusableCellWithIdentifier: kIdentifier];
if (cell == nil) {
//Init cell, only init
}
//Setup the cells detail, such as the textField.text and so on
return cell;
}

iOS: UITableView clarifications

How can I eliminate views created upon selecting a cell in UITableView when the cell is no longer selected or other cell is on selection.
Suppose I have the code below for didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;
UIView *selectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (int)cellSize.width, (int)(cellSize.height + 100))];
selectionView.layer.borderWidth = 2;
[[tableView cellForRowAtIndexPath:indexPath]addSubview:selectionView];
}
Now, I want to remove that created selectionView when I focus on another cell and create it again to the cell which I am focusing.. The problem is that when I select a cell for the first time, it works perfectly but when I select another cell, the selectionView created from the previous cell still does not disappear and it duplicates the view already. How am I suppose to solve this? Need suggestion.. :( thanks..
You need to add tag for the selectionView as follows
selectionView.tag = 100;
Also , you need to have the reference of the last selected indexPath by declaring a NSIndexPath class member and retaining it.
So while selecting a new cell, get the cell with last selected indexpath and remove the view from the cell as follows
UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelIndexPath];
UIView *view = [lastCell viewWithTag:100];
[view removeFromSuperview];
You really need to look into subclassing UITableViewCell if you want a custom selection UI. Then override [UITableViewCell setSelected:animated:] (or [UITableViewCell setHighlighted:animated:]) to perform your customisations.

problem in cell.accessoryType in UITableView

In my app if user press on any cell in UITableView then accessoryType of cell will be set to check mark like following
-(void)Check:(UITableView *)tableView Mark:(NSIndexPath *)indexPath
{
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
buttonCount++;
[selectedCellArray addObject:indexPath];
}
and if user press the same cell then uncheck will happen as follows
-(void)UnCheck:(UITableView *)tableView Mark:(NSIndexPath *)indexPath
{
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
buttonCount--;
if (buttonCount == 0) {
[selectedCellArray removeAllObjects];
}
}
and i am calling this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{
[self UnCheck:tableView Mark:indexPath];
}
else
{
[self Check:tableView Mark:indexPath];
}
Problem is when i am pressing on 1st cell it call Check method and mark the cell to but when i am scroll down i find 2-3 more cheked cell ...even i did not select those cell...i dont know why and how it checked automatically ...
i hope some one know where is the problem
thank you very much
Because cells will be reused by the tableview. Also set the checkmark/accessory type in your tableView:cellForRowAtIndexPath: method.
ya.. I see this happen a lot.
the correct pattern to manage the UITableViewCell state is NOT to directly manipulate the TableViewCell to update the UI, and always set, draw and create the correct state from cellForRowAtIndexPath (or tableView:willDisplayCell:forRowAtIndexPath:)
Meaning, if the user clicks on the cell and you need to update the UI of that cell, store the new state of that cell in an array or dictionary (I find that an NSMutableDictionary with the NSIndexPath as the key works very well).
then call the reloadRowsAtIndexPaths:withRowAnimation: or just [tableView reloadData] so that the cellForRowAtIndexPath reads that array or Dictionary and correctly draws the cell.
otherwise, the cellForRowAtIndexPath will constantly overwrite your changes and use recycled cells that hold incorrect state.
one exception to this rule would be if you would like a nice animation between the two states... if that is the case, save off your new state, perform your animation right there on the cell, then when the animation completes, call the same reloadRowsAtIndexPaths:withRowAnimation or reloadData so that the cell is redrawn in its new state.

How to empty out the whole tableview on a button click in iPhone SDK?

In my iPhone app, I am using a single tableview to display different sets of data based on the button clicked.
Now as I am using the same tableView I need to blank out the tableView contents everytime a new button is selected.
And this is quite normal requirement rite? As such it is inefficient to take 7 tables to show 7 different data sets.
Problem:
I have seen that table clears out but when we display some other data in the table then the previous data appears in background as in Screenshot AFTER.
I have tried setting the array as nil and reloading the tableView but then it doesnt seem to work.
What can be a fix for this issue?
I have checked the code and it seems proper to me.
You can refer to the Screen shot to get a better idea of what actually is happening.
BEFORE ( i.e. the first time Event is clicked)
AFTER (i.e. once the Event category button is clicked after some other category button)
You can clearly see a different image in background where as it should be same as image in above screenshot. This is not a button, I am adding a UIImageView to tableViewCell.
NSArray is not mutable, that is, you cannot modify it.
Instead of using NSArray use NSMutableArray and use
[mutArr removeAllObjects];
and then reload the tableView. It worked for me.
- (IBAction)yourAction {
tableView.delegate = nil;
tableView.dataSource = nil;
[tableView reloadData];
}
it will empty your table view... ur need is not clear
In button action method, call the tableView reload and assign null to object from which you are initializing the cells previously,
[tableView reloadData];
You can return zero for numberOfRowsInSection, and add BOOL variable for isEmpty.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(isEmpty){
return 0;
}else{
// your current logic
}
}
//action when button clicked
-(IBAction)myAction{
isEmpty = TRUE;
[self reloadData];
}
I don't think it will work if your array is nil. Try initializing it as a empty array with:
myArr = [NSArray array];
And then reload the tableView data. Otherwise I think we need to see your code
EDIT
It is still a bit unclear(still no code in your question), but I think your problem is really related to your cell construction. Are you adding UIImageView on every cellForRowAtIndexPath message?
I guess you have something similar to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cellName";
myCell *cell = (myCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//CONSTRUCT CELL.
//THIS IS THE ONLY PLACE WHERE YOU SHOULD ADD SUBVIEWS TO YOUR CELL
}
//Are you adding subviews here? -you shouldn't
//configure data in cell
return cell;

First Tap on customcell of uitableview should expand it and second should contract it

In my application I have this requirement that first tap on custom cell of uitableview with a label in it should expand it and second should contract it. I'm able to expand and contract cell and expand label inside cell, but not able to contract the label on second tap.
I'm using this function
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if( selected == YES ) {
[self expandRow];
}
else {
[self contractRow];
}
height = [lblFeed frame].size.height + 75;
}
expandRow expands the label and contractRow contracts it. I'm perplexed as for how many rows this function gets called. It doesn't get called only for the cell tapped, it gets called more number of times for single tap on single cell may be for other cells but I'm not getting which rows.
This' really urgent.
Can anybody please help?
Tapping a selected row doesn't cause it to be deselected. When a cell gets selected, it stays selected until deselectRowAtIndexPath:animated: gets called on its table. That's why your method isn't getting called for the second tap.
In an MVC architecture like UIKit, it's recommended that you handle user interactions in your controller classes. It would be appropriate to override -[UITableViewCell setSelected:animated:] if all you were doing was customizing the way the view represents a selected cell, but in this case your expand/contract toggle behavior would require a change in the way UITableView selects and deselects its cells.
You could subclass UITableView and implement this toggle behavior yourself, or you can leave UITableView alone and handle it all at the UIViewController level by doing something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.expandedIndexPath isEqual:indexPath]) {
[(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] contractRow];
self.expandedIndexPath = nil;
}
else {
if (self.expandedIndexPath) {
[(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:self.expandedIndexPath] contractRow];
}
[(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] expandRow];
self.expandedIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
I would suggest that you don't add your functionality on top of the selected property of the cell, which has slightly different behaviour than you expect.
Just add your own BOOL expanded property, and see how that works. You should probably call it from the UITableView delegate methods, too.