Can't hide the custom checkmark in the didSelectRowAtIndexPath - iphone

I tried to put a custom UIButton with an image on a custom tableview cell to use it as checkmark, 1st tap, hides the checkmark and next tap brings it back... i'm trying the below code "example code" to perform this function but its not hiding the checkmark i.e."custom UIButton".
not sure what am i missing here?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",indexPath);
TableViewCell *customCell = [[TableViewCell alloc]init];
if (customCell.checkMarkButton.hidden == NO) {
customCell.checkMarkButton.hidden = YES;
} else {
customCell.checkMarkButton.hidden = NO;
}
}

Replace this line: TableViewCell *customCell = [[TableViewCell alloc]init]; with this:
TableViewCell *customCell = [tableView cellForRowAtIndexPah:indexPath];. You don't have to crate a new cell, just get the one that was tapped.

The problem is that in didSelectRowAtIndexPath you alloc a new cell.
Try this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *customCell = (TableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
customCell.checkMarkButton.hidden = !customCell.checkMarkButton.hidden;
}

Related

How to use user input from UITextField to add a new UITableViewCell with an image?

I am becoming increasingly frustrated as I can not figure out how to get this to work.
I have searched and searched and nothing seems to be helping me out to get this to do what I want.
What I am trying accomplish: I am trying to get the Task that the user will type in using the UITextField on top to add a new cell with their input as the text and with a check box to the left of it. When the cell is tapped, I would like to have the check box change to one with a check mark within it and for the entire cell to change opacity to 50%. Also, I would like to make the cell pop up with a delete button when it is swiped from left to right.
How would I make this all happen? I currently just have the UITextField added to the .xib using the Attribute Inspector to make it look the way it does. I also have a tool bar up top that I have used the Attributes Inspector to modify as well.
Nothing is coded to connect these to actions, they are just sitting in the .xib.
Here is an image of the rendering I created. (What I am trying to get)
http://i.imgur.com/rTKnvud.png
Here is what I have so far in Xcode in the .xib.
http://i.imgur.com/wfcVOrY.png
Thank you in advanced,
Jacob
You need to either add button to cell programatically or create a custom cell. When user taps on cell or checkbox you can change button's image to selected checkbox. for proper display of checkbox take a NSMutableArray and use the below code.
Note
1. specialNeedsList is a array which I'm using to display data on cell.
2. selectedSpecialNeed is a Mutable Array which I'm using to store selected/checked values
3. I'm using checkbox button for UITableViewCell accessory view. IN your case code will be little bit different.
Follow this tutorial Get row for button tapped as well along with my code. You will get idea for accomplish this. Whatever I'm doing on - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
you need to do this on UIButton's action.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger returnValue = 1;
return returnValue;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger returnValue = 0;
returnValue = ([self.specialNeedsList count] > 0) ? [self.specialNeedsList count]:0;
return returnValue;
}
- (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];
UIButton * btnShare = [[UIButton alloc] initWithFrame:CGRectMake(0,0,30,30)];
[btnShare setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[btnShare setImage:[UIImage imageNamed:#"checkboxChecked.png"] forState:UIControlStateHighlighted];
[btnShare setImage:[UIImage imageNamed:#"checkboxChecked.png"] forState:UIControlStateSelected];
[btnShare addTarget: self action: #selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
cell.accessoryView = btnShare;
[btnShare release];
}
NSString* subAreaOfStudy = [self.specialNeedsList objectAtIndex:indexPath.row];
if (self.specialNeedsList && [self.specialNeedsList count] > 0) {
[cell.textLabel setFont:[UIFont systemFontOfSize:15.0f]];
cell.textLabel.text = [self.specialNeedsList objectAtIndex:indexPath.row];
}
BOOL isShared = ([self.selectedSpecialNeeds count] > 0 && ([self.selectedSpecialNeeds indexOfObject:subAreaOfStudy] != NSNotFound));
UIButton* btnShare = (UIButton*)cell.accessoryView;
btnShare.selected = isShared;
[btnShare setNeedsDisplay];
return cell;
}
- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event{
NSIndexPath * indexPath;
indexPath = [tblVSpecialNeeds indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView:tblVSpecialNeeds]];
if ( indexPath == nil )
return;
[self tableView:tblVSpecialNeeds accessoryButtonTappedForRowWithIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSString *draft = [self.specialNeedsList objectAtIndex:indexPath.row];
BOOL recordAlreadySelected = ([self.selectedSpecialNeeds indexOfObjectIdenticalTo:draft] != NSNotFound);
if(recordAlreadySelected) {
[self.selectedSpecialNeeds removeObject:draft];
}
else {
[self.selectedSpecialNeeds addObject:draft];
}
[tblVSpecialNeeds reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row > [self.specialNeedsList count]-1) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
[self tableView:tblVSpecialNeeds accessoryButtonTappedForRowWithIndexPath:indexPath];
}
To delete on swipe, use Tableview's Delegate method:
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
[arrAddData removeObjectAtIndex:indexPath.row];
[tbldata reloadData];

How to display a custom UITableViewCellAccessoryCheckmark

I have a table view that needs a custom UITableViewCellAccessoryCheckmark. The checkmark displays when a row is selected and disappears when anther row is selected and then appears on the last most selected view. That works fine.
the problem arises when I use this line:
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"icon-tick.png" ]];
to add a custom UITableViewCellAccessoryCheckmark. After that code the UITableViewCellAccessoryCheckmark remain on all rows and don't disappear when another row is touched.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int index = indexPath.row; id obj = [listOfItems objectAtIndex:index];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSLog(#"%d",indexPath.row);
if (rowNO!=indexPath.row) {
rowNO=indexPath.row;
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"icon-tick.png" ]];
[self.tableView cellForRowAtIndexPath:lastIndexPth].accessoryType=UITableViewCellAccessoryNone;
lastIndexPth=indexPath;
}
A much cleaner and cooler way would be to overwrite UITableViewCell like this:
- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType
{
// Check for the checkmark
if (accessoryType == UITableViewCellAccessoryCheckmark)
{
// Add the image
self.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YourImage.png"]] autorelease];
}
// We don't have to modify the accessory
else
{
[super setAccessoryType:accessoryType];
}
}
If you have done that, you can continue using UITableViewCellAccessoryCheckmark because your class will automatically replace it with an image.
You should only set the style in your cellForRowAtIndexPath method. Like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// [init subclassed cell here, dont forget to use the table view cache...]
cell.accessoryType = (rowNO != indexPath.row ? nil : UITableViewCellAccessoryCheckmark);
return cell;
}
And then, you just have to update rowNO in didSelectRowAtIndexPath to update your data and redraw the cell, like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (rowNO != indexPath.row)
{
rowNO = indexPath.row;
}
[self.tableView reloadData];
}
Also, instead of reloading the whole table with [self.tableView reloadData], you could only reload the two cells that change their style (e.g. checkmark) using reloadRowsAtIndexPaths.
Hm don't know why but i can't add comments so im writing this as answer. The problem with Blauesocke answer is that the AccessoryType wil be not set to UITableViewCellAccessoryCheckmark so you can't check the cell AccessoryType. Is there some way to do it right so the cell AccessoryType will be corect type just another image.
I'm using the method like this :
- (void)setAccessoryType:(UITableViewCellAccessoryType)newAccessoryType
{
[super setAccessoryType:newAccessoryType];
// Check for the checkmark
switch(newAccessoryType)
{
case UITableViewCellAccessoryCheckmark:
self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yorCheckmark.png"]];
break;
case UITableViewCellAccessoryNone:
self.accessoryView = nil;
break;
default:
break;
}
}

how to add button on uitableviewCell and delete previous on next cell selection

In my uitableview I want to add two buttons on cell, When user selects some particular cell. Now if user selects another cell then button should be deleted from the last selected cell and added to the new selected cell.
Keep a reference to the button at class level.
When you want to move the button call [self.myMovingbutton removeFromSuperview] and then [cell addSubview:self.myMovingButton].
Subclass UITableViewCell and add the UIButton as a property on the subclass. Use that property to store your button instance. When re-using the cell you can check the value of the property to see if you need to remove the old button before adding the new one.
.h
NSIndexPath *selectedIndexPath;
.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// . . Some Code . . .
if ( selectedIndexPath.row = indexPath.row && selectedIndexPath.section = indexPath.section ) {
button1.hidden = NO;
button2.hidden = NO;
} else {
button1.hidden = YES;
button2.hidden = YES;
}
// . . Some Code . . .
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndexPath = indexPath;
[tableView relaodData];
}

iOS 6: UITableView Edit and Autoresizing

Hi Could someone please help me to understand how to layout table cell's components automatically while editing in iOS 6.0? I have set AutoLayout FALSE for UITableViewCell Autosizing option set to top right in Attributes Inspector! Cell's right side imageviews are overlapping by delete buttons. Please refer attached image. Following is the code for this. Is there anyway I can fix this issue?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:#"PlayerCell"];
Player *player = [self.players objectAtIndex:indexPath.row];
cell.nameLabel.text = player.name;
cell.gameLabel.text = player.game;
cell.ratingImageView.image = [self imageForRating:player.rating];
return cell;
}
- (UIImage *)imageForRating:(int)rating
{
switch (rating)
{
case 1: return [UIImage imageNamed:#"1StarSmall.png"];
case 2: return [UIImage imageNamed:#"2StarsSmall.png"];
case 3: return [UIImage imageNamed:#"3StarsSmall.png"];
case 4: return [UIImage imageNamed:#"4StarsSmall.png"];
case 5: return [UIImage imageNamed:#"5StarsSmall.png"];
}
return nil;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.players removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I have added following delegate methods and it works fine when I swipe the cell.
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = YES;
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = NO;
}
...but this methods doesn't get invoked when I press editButtonItem Button! That's strange! I am missing something. I have added following method that gets invoked when Edit/Done button pressed but there is no way to detect the cell that will be selected for editing!
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing)
{
NSLog(#"editMode on");
}
else
{
NSLog(#"Done leave editmode");
}
}
When user clicks left round button, is there anyway to add selector on that button click and get the cell index?
Your ratingImageView uses autoresizing masks to resize and position itself when you are not using AutoLayout. By default this means that the distance to the left and top is fixed and the size won't change.
When you toggle a cell to edit the contentView moved and resizes but your ratingImageView stays fixed to the top and left. You can visualize this by temporarily (for learning purposes) set a background color to the cells contentView and see how it resizes as you edit and delete the cell.
What you want is for your rating view to stay a fixed distance from the right edge instead of the left. You can either change this in InterfaceBuilder (where you do your XIBs or Storyboard) or in code by setting the autoresizingMask property of the ratingImageView.
Go to this tab
Change the autosizing like this
Or do it in code
// in code you specify what should be flexible instead of what should be fixed...
[ratingImageView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];

how to remove invisible tableview cell's downloaded image . in ios

I want to remove downloaded image in invisible tableview cell when I scrolled tableview.
So I set a tag
- (void)appImageDidLoad:(NSIndexPath *)indexPath
{
int row=(int)indexPath
UITableViewCell *cell;
cell=[self.gamesTableView cellForRowAtIndexPath:indexPath];
OneItemCell *cell1=(OneItemCell*) cell; //custom cell
cell1.imageview.tag= row+100;
}
I set remove downloaded image when selected row is last row cell(=add cell).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row==nArrayCount)
{
if(row>19)
{
for(int i=1;i<row-8;i++)
{
[[self.gamesTableView viewWithTag:i+100] removeFromSuperview];
}
}
}
but it is not working . because [[self.gamesTableView viewWithTag:i+100] is NULL.
another way I set a key to cell's imageView by NSMutableDictionary(=imageRemoveInProgress)
- (void)appImageDidLoad:(NSIndexPath *)indexPath
{ .
.
[imageRemoveInProgress setObject:cell1.imageView forKey:[NSNumber numberWithInt:row+100]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ .
.
[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] removeFromSuperview];
}
it is not working too . because[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] is NULL too.
[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] has value in appImageDidLoad.
but In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
it has NULL.
why do taged and seted key value cell's imageView become NULL?
how to remove tableViewcell's imageview?
I don't know the exact way, But I can provide a hint to do it
you can use something like this
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
visiblePaths is an array of indexPaths of cells that are currently visible, you can check whether the cell for which you want to remove the image lies in this array or not.