UITableViewCell not showing checkmark accessory - iphone

I have a UITableView that gets shown when a button is tapped and it should display a checkmark accessory on a cell which is determined using a specific string from an array (the array that populates the table view) based on its index. I have the following code in the viewDidLoad method:
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[arrayOfSounds indexOfObject:currentSound]];
UITableViewCell *cell = [tableViewProperty cellForRowAtIndexPath:indexPath];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
but some why it doesn't display the proper checkmark.
The currentSound variable is a string and it does have the correct text as I checked during debugging. Please advise!

You should use the tableView's
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation (UITableViewRowAnimation)animation;
to reload the cell.

Related

Determine UIButton in UITableViewCell

I have UIButton in every UITableViewCell. How can I check in which cell is button pressed?
Button is added in custom class of UITableViewCell (not in cellForRowAtIndexPath).
How it works: I have thumbUp in every UITableViewCell (grouped Tableview) and I need to know for which cell is thumbUp pressed. I can see max 2 cells at a time (because cell height is 400). I tried increment counter every time cellForRowAtIndexPath is called. It works for 1st cell but when I scroll down and cellForRow is called for second cell, counter is raised, but thumbUp button of first cell is still visible and if I press that thumbUp, it will be like 2nd cell thumbUp is pressed. Hope you understand.
Need help !!! Thanks
try this
- (IBAction)buttonWasPressed:(id)sender
{
NSIndexPath *indexPath =
[self.myTableView
indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
// Do something with row index
}
or give tag to every UIButton in cellForRowAtIndexPath method
and get cell object
- (IBAction)buttonWasPressed:(id)sender
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:sender.tag];
}

Where to use scrollToRowAtIndexPath in iphone?

I am using [self.messageList scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; in cellForRowAtIndexPath and it is working fine, when a row is added in table array then it auto scroll upside but the problem is that when I try to see the top rows then it automatically come again last rows. And I am not able to see my all rows. Please let me know where I use this or any other approach to do this.
cellForRowAtIndexPath method is not called for all cell that you have. It is only called for visible cell in your TableView. When You Scroll UITableView then it ask to UITableView that you want to use reusable cell or add to new one. generally we use reusable cell so, it is use 1st no of cell and add at end of cell . it is not create another new cell at the end. so, when you scroll your UITableView it not scroll to end of cell it indexPath is similar to indexPath of first no of cell. so you can not scroll your UITableView at to Down.
use following code may be helpful for you.... :)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[self.tblView scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionTop animated: YES];
// your code
}
// your code
return cell;
}
If you write this code in your cell for row at index path, then it will get called every time you try to scroll manually coz this message is called to draw your cell and you will b directed to the row you specified. Try checking your indexpath in a IF--Else block and scroll only when a certain action happens.
cellForRowAtIndexPath is a method for creating a cell to view .When you scroll this method is called when a cell is viewed.So each time the method called the [self.messageList scrollToRowAtIndexPath:indexPath method(which is called inside cellForRow) is also called and the table scrolls down to that row!!!

How to access the cell in tableview?

I am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.
I have tried this
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
But this crashes since cell is custom.
Can any one suggest me a good method to do that?
Instead of using
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
try using
YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Hope it helps
First of all you need to define a tag -
#pragma imageViewTag 1
then in your cellForRowAtIndexPath assign this tag to your image view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
then you can access your image view any where with the help of this tag as -
UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
In your TableView DidSelect Delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [cell.contentView viewWithTag:15];
//You should have set the tag value as 15 when creating the image in cell
//and you should have added to the cell contentview
}
If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.
If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.

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.

I want to add check sign in row when any row is selected in UITableView

I want to add a check sign when any row is selected in table view please help
Implement tableView:didSelectRowAtIndexPath: in your table view delegate, grab the cell that was selected using [tableView cellForRowAtIndexPath:indexPath] and set its accessory to UITableViewCellAccessoryCheckmark.
If you want to implement something similar to the multi-value table views in the Settings app, what I do is use a property to keep track of what's already selected:
#property (nonatomic, retain) NSIndexPath *selectedIndexPath;
In the same delegate method, I remove the checkmark from the cell at the selection, add the checkmark to the cell that was tapped and update the property with the index of that cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
[oldCell setAccessory:UITableViewCellAccessoryNone];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
[newCell setAccessory:UITableViewCellAccessoryCheckmark];
[self setSelectedIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
To go one step further, you can find a way to persist the index path in your app (possibly using NSUserDefaults, and in tableView:cellForRowAtIndexPath: apply the checkmark to whichever cell that corresponds to that index path, so your checkmark shows up on view load.
http://iphonedevelopment.blogspot.com/2008/10/table-view-multi-row-edit-mode.html