I want Checkmark in particular TableView Cell.
So I have used code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
newRow = [indexPath row];
oldRow = [lastIndexPath row];
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
else{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
lastIndexPath = indexPath;
}}
Its working in iPhone Simulator fine.
But while testing in iPhone device, it crashes the application.
Any solution for this..?
Thanks in advance.
The most likely cause of your crash is in your ivar, lastIndexPath. You're storing values in this without retaining them, so they may be released at any time. Try defining a property named lastIndexPath (retain for manual reference counting, strong for automatic reference counting). Then you can use self.lastIndexPath = indexPath or [self setLastIndexPath:indexPath].
Also, it's bad to forcibly change a cell contents like this. It's better to store the selected index, then reload the table data. For maximum efficiency, only update the changed cells. Have your cellForRowAtIndexPath: method switch the checkmarks on and off.
Simply write following code in "cellForRowAtIndexPath" method.
[[cell imageView] setImage:[UIImage imageNamed:#"tickmarkBlue.png"]];
[[cell imageView] setHidden:YES];
if(<your condition here>)
{
[[cell imageView] setHidden:NO];
}
Related
I need to enable multiple selection in my tableview and also i need to keep track of what i have selected (like save it to an array or something). My approach so far;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[arrayobject objectAtIndex:indexPath.row];
bool xx = [[allmyselectedobjects objectAtIndex:indexPath.row] containsIndex:1];
if (xx) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
and the didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.cuisineTableView cellForRowAtIndexPath:indexPath];
if ([cell accessoryType] == UITableViewCellAccessoryNone) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[self.allmyselectedobjects insertObject:1 atIndex:indexPath.row];
}
else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
[self.allmyselectedobjects insertObject:0 atIndex:indexPath.row];
}
}
I could click multiple records, but when i scroll down i see other cells also ticked with the check mark (which i didn't select). I have been trying this for days now, can someone help me fix this code ?
A given cell instance is recycled and used for many rows. Store your checked indexPathes but not the cells.
Right before
Bool xx =
add this:
[cell setAccessoryType:UITableViewCellAccessoryNone];
It seems you are using an NSIndexSet to store checked cells. So just insert the index of the selected cell and test it.
// Test
BOOL xx = [allmyselectedobjects containsIndex: indexPath.row]
// Selected cell
[allmyselectedobjects addIndex: indexPath.row]
// Unselected cell
[allmyselectedobjects removeIndex: indexPath.row]
I've done this before, but am having trouble getting it to work again. In any case, at my model class I basically have a dictionary that has two items for Sorting, and I want only one value to be YES, and the others to be NO. I want the selection of the item in the UITableViewCell to reflect that and animate the selection and deselection. So at the model class, I have this method that just changes the other value that was selected to false:
- (void)setSingleSort:(NSString *)key {
for (NSString *str in [_sortOrderDictionary allKeys]) {
if (str != key) {
[_sortOrderDictionary setObject:[NSNumber numberWithBool:NO] forKey:str];
}
}
}
Then I have this snippet in my didSelectRowForIndexPath method:
NSUInteger row = [indexPath row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell setSelected:YES animated:YES];
NSString *key;
BOOL valueAtKey;
key = [_sortCategoryList objectAtIndex:row];
valueAtKey = [[dmgr.SortOrderDictionary valueForKey:key] boolValue];
if (valueAtKey != YES) {
[dmgr.SortOrderDictionary setObject:[NSNumber numberWithBool:!valueAtKey] forKey:key];
[dmgr setSingleSort:key];
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:_lastIndexPath animated:YES];
}
_lastIndexPath = indexPath;
[tableView reloadData]; //Edited, added this to the end and it seems to work. Don't know why it's needed though since the deslectRowAtIndexPath should animated, along with the setSelected:Animated: right?
I think your selectionStyle is the reason. This is normally how I go about doing this. In cellForRowAtIndexPath method:
//create new or fetch dequeued cell
if((indexPath.row == self.indexPathOfCurrentValue.row) && (indexPath.section == self.indexPathOfCurrentValue.section))
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
then in the didSelectRowAtIndexPath method, do something like this:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//APP RELATED LOGIC GOES HERE
if(nil != self.indexPathOfCurrentValue)
[[tableView cellForRowAtIndexPath:self.indexPathOfCurrentValue] setAccessoryType:UITableViewCellAccessoryNone];
self.indexPathOfCurrentValue = indexPath;
[[tableView cellForRowAtIndexPath:self.indexPathOfCurrentValue] setAccessoryType:UITableViewCellAccessoryCheckMark];
This is normally the approach I take when I need to checkmark the row on user's tap and uncheck the last selected row. Obviously, the currentValue index path variables holds the lastest checkmark's index path.
Get rid of this line:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I have a selection that will select just 1 row of category.
but I will like it to select Spirits row when loaded.
Something like this:
At the moment it comes to this without selecting anything:
Where shall I do the compare for the in order for it to selectRowAtIndexPath?
// Customize the appearance of table view cells.
- (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] autorelease];
}
categoryString = [arrayCategory objectAtIndex:indexPath.row];
cell.textLabel.text = categoryString;
if (cell.textLabel.text == categoryString) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* newCell = [tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
newCell.highlighted =YES;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
oldCell.highlighted = NO;
}
[tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
}
Use selectRowAtIndexPath:animated:scrollPosition: method to select a row programatically.
Few things to correct in your code. You should use isEqualToString: method to match the strings, like this, if ([cell.textLabel.text isEqualToString:categoryString]) {. Next thing is, you are assigning the categoryString to cell.textLabel.text, and on the next line you are matching them, so it would always return YES. I think you are matching the wrong values.
You have to manually set multiple check marks. for that you can use UIImageview for each UITableViewCell and as per the stored old data, you have to set check marks in didSelectRowAtIndexPath(delegate method of UITableView).
in my application I have UITableView & i used
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
and when I scrolled this table then I get multiple checkmark in multiple cell
please help me..
In general, the cell come from 2 ways:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Cell";
UITableViewCell *cell = ... // The cell come from reuse queue.
if (cell == nil) {
cell = ... // if reuse queue has no cell, then create an autoreleased cell
}
// Configure cell by indexPath.
// You should configure cell HERE.
}
Because your cell maybe come from reuse queue, it was configured. Obviously, you forget to re-configure those cells which come from reuse queue.
you can use this sample code on tableView's delegate method didSelectedRowAtIndexPath
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:YES animated:YES];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
for(int iterator=0;iterator<3;iterator++)
{
UITableViewCell *eachCell = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:iterator inSection:0]];
[eachCell setSelected:NO animated:YES];
[eachCell setAccessoryType:UITableViewCellAccessoryNone];
}
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
[newCell setSelected:YES animated:YES];
[newCell setAccessoryType:UITableViewCellAccessoryCheckmark];
I'm using a UITableView to allow selection of one (of many) items. Similar to the UI when selecting a ringtone, I want the selected item to be checked, and the others not. I would like to have the cell selected when touched, then animated back to the normal color (again, like the ringtone selection UI).
A UIViewController subclass is my table's delegate and datasource (not UITableViewController, because I also have a toolbar in there).
I'm setting the accessoryType of the cells in cellForRowAtIndexPath:, and updating my model when cells are selected in didSelectRowAtIndexPath:. The only way I can think of to set the selected cell to checkmark (and clear the previous one) is to call [tableView reloadData] in didSelectRowAtIndexPath:. However, when I do this, the animating of the cell deselection is weird (a white box appears where the cell's label should be). If I don't call reloadData, of course, the accessoryType won't change, so the checkmarks won't appear.
I suppose I could turn the animation off, but that seems lame. I also toyed with getting and altering the cells in didSelectRowAtIndexPath:, but that's a big pain.
Any ideas? Abbreviated code follows...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kImageCell];
if( aCell == nil ) {
aCell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:kImageCell];
}
aCell.text = [imageNames objectAtIndex:[indexPath row]];
if( [indexPath row] == selectedImage ) {
aCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
aCell.accessoryType = UITableViewCellAccessoryNone;
}
return aCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
selectedImage = [indexPath row]
[tableView reloadData];
}
I solved it like hatfinch suggested in his edit. My code sample make sure there's only one row checked but I'm sure you can tweak it if thats not what you need.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I had this exact same problem and never solved it.
In the end it was moot for me because I had to listen for changes in the model (the selection could change outwith the user's control if a connection was lost) so I just changed the model and let the notification reload the data, but it didn't look too pretty (it was like a non-animated deselection, but one run loop iteration late!)
I suspect the only way to do it is, as you suggest, keeping a cache of references to the cells themselves. This shouldn't be too messy if you write a UITableViewCell subclass to encapsulate the logic, overriding -prepareForReuse.
Still, not pretty, and if the ringtone guys at Apple had to do it this way, they ought to have pressed the UITableView team harder to fix it!
EDIT:
Well, what do you know?! This is really easy after all. -[UITableView cellForRowAtIndexPath] will give you the actual cells (or nil if they're offscreen) and you can just change the accessoryType.
I have a cleaner way of doing it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
I'm using this simple and nice working mechanism with reloadRowsAtIndexPaths.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *rowsArray = [NSArray arrayWithObjects:indexPath, selectedIndexPath, nil];
self.selectedIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:rowsArray withRowAnimation:UITableViewRowAnimationNone];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
.... skipped usual code .....
if ([selectedIndexPath compare:indexPath] == NSOrderedSame) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
This will work as desired...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow == oldRow){
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
lastIndexPath = indexPath;
}
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:index];
oldCell.accessoryType = UITableViewCellAccessoryNone;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
index = indexPath;
}