Hide MenuView on Cell - objective-c

I have a button on cell On click of this button I want to open a view, which work fine. Now problem is when we click on next cell button, my previous view doesn't hides.
Here is what I am trying two way but not work. Please help .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustumCellTableViewCell";
CustumCellTableViewCell *cell = (CustumCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustumCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.touchButton.tag =indexPath.row;
[cell.touchButton addTarget:self action:#selector(menuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.menuView.hidden = YES;
return cell;
}
- (void) menuButtonClick:(id) sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
CustumCellTableViewCell *cell=(CustumCellTableViewCell *)[table cellForRowAtIndexPath: indexPath];
if (cell.menuView.hidden) {
cell.menuView.hidden = NO;
} else {
cell.menuView.hidden = YES;
}
1 method
for ( CustumCellTableViewCell *cell1 in table.visibleCells ) {
NSLog(#"%#",cell1);
if (cell1 != cell) {
cell.menuView.hidden = YES;
}
}
2 method
for (NSIndexPath *indexpaths in [table indexPathsForVisibleRows] ) {
if (indexpaths == indexPath) {
cell.menuView.hidden = NO;
}
else
{
cell.menuView.hidden = YES;
}
}
}

You are using currently selected cell. If you want to hide last selected cell menu than you have to store last selected cell index.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastSelectedCell inSection:0];
CustumCellTableViewCell *cell=(CustumCellTableViewCell *)[table cellForRowAtIndexPath: indexPath];
cell.menuView.hidden = YES;
/*
your code goes here
*/
lastSelectedCell = [sender tag];

[for ( CustumCellTableViewCell *cell1 in \[table visibleCells\] ) {
if (cell1 != cell) {
cell1.menuView.hidden = YES;
}
else
{
cell1.menuView.hidden = NO;
}
}][1]
[1]: http://

you can add cell tag with indexPath.row and compare cell.tag is equal to [sender tag] or not. Also check your sender tag value is correct or not.
- (void) menuButtonClick:(id) sender
{
[table reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
CustumCellTableViewCell *cell=(CustumCellTableViewCell *)[table cellForRowAtIndexPath: indexPath];
if(cell.tag==[sender tag]){
cell.menuView.hidden = NO;
} else {
cell.menuView.hidden = YES;
}
}

Related

Selected row changes when scrolling UITableView

currently my situation is my fist row is checked by default. but when i want to uncheck it, it need user to click twice to uncheck it. may i know where i done wrongly?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"Report_SelectGroupTableViewCell";
Report_SelectGroupTableViewCell *cell = (Report_SelectGroupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Report_SelectGroupTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.indexPath = indexPath;
cell.delegate = self;
}
// [myTableView selectRowAtIndexPath:0 animated:NO scrollPosition:UITableViewScrollPositionNone];
//[self tableView:myTableView didSelectRowAtIndexPath:0];
[cell setSelectionStyle:NO];
NSString *groupID = [[groupArray objectAtIndex:indexPath.row] objectForKey:#"group_id"];
if ([groupID isEqualToString:selectedGroup]){
NSMutableDictionary *dict = [groupArray objectAtIndex:indexPath.row];
BOOL isSelected = [[dict objectForKey:#"isSelected"] boolValue];
if (!isSelected) {
isSelected = !isSelected;
[dict setObject:[NSNumber numberWithBool:isSelected] forKey:#"isSelected"];
}
cell.userInteractionEnabled = NO;
}
cell.groupName.text = [[groupArray objectAtIndex:indexPath.row] objectForKey:#"group_name"];
NSMutableDictionary *typeDict = [groupArray objectAtIndex:indexPath.row];
cell.tickButton.selected = [[typeDict objectForKey:#"isSelected"] boolValue];
if (cell.tickButton.selected) {
[cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
if([cell.groupName.text isEqual:#"All Users"])
{
[cell.tickButton setImage:[UIImage imageNamed:#"tick_Icon_empty.png"] forState:UIControlStateNormal];
}
}
else{
[cell.tickButton.layer setBorderColor:[UIColor whiteColor].CGColor];
}
if([cell.groupName.text isEqual:#"All Users"])
{
[cell.tickButton setImage:[UIImage imageNamed:#"tick_Icon.png"] forState:UIControlStateNormal];
[cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
[typeDict setObject:#"1" forKey:#"isSelected"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Report_SelectGroupTableViewCell *cell = (Report_SelectGroupTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dict = [groupArray objectAtIndex:indexPath.row];
BOOL isSelected = [[dict objectForKey:#"isSelected"] boolValue];
isSelected = !isSelected;
[dict setObject:[NSNumber numberWithBool:isSelected] forKey:#"isSelected"];
if (cell.tickButton.isSelected == YES)
{
[cell.tickButton setSelected:NO];
[cell.tickButton.layer setBorderColor:[UIColor whiteColor].CGColor];
if([cell.groupName.text isEqual:#"All Users"])
{
[cell.tickButton setImage:[UIImage imageNamed:#"tick_Icon_empty.png"] forState:UIControlStateNormal];
}
}
else if (cell.tickButton.isSelected == NO)
{
[cell.tickButton setSelected:YES];
[cell.tickButton.layer setBorderColor:[UIColor clearColor].CGColor];
}
}
You should remove
[myTableView selectRowAtIndexPath:0 animated:YES scrollPosition:
UITableViewScrollPositionNone];
and use this to select the first row (this should be called once outside this method or you want to set this row selected everytime each cell is render)
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
And I don't know why you called this in this method
[myTableView.delegate tableView:myTableView didSelectRowAtIndexPath:0];
but if you want to call it you should change to this too
[myTableView.delegate tableView:myTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
ps. You should set the cell's selectionStyle correctly to see the cell is selected.
Update::
Try to replace all codes in the method with this
Report_SelectGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Report_SelectGroupTableViewCell"];
if (!cell){
cell = [[[NSBundle mainBundle] loadNibNamed:#"Report_SelectGroupTableViewCell" owner:self options:nil] objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
return cell;
Please tell me if the first row is not selected and highlighted.

How do clickable checkmark accessory and none accessory?

I have simple code in my TableViewController:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(debug == 1){
NSLog(#"Running %# '%#'",self.class, NSStringFromSelector(_cmd));
}
static NSString *cellIndetifier = #"Student Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier
forIndexPath:indexPath];
Student *student = [self.frc objectAtIndexPath:indexPath];
NSMutableString *title = [NSMutableString stringWithFormat:#"%#", student.name];
[title replaceOccurrencesOfString:#"(null)"
withString:#""
options:0
range:NSMakeRange(0, [title length])];
cell.textLabel.text = title;
if (cell == nil) {
cell.textLabel.textColor = [UIColor whiteColor];
}
if ([self.checkedIndexPaths containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
if(debug == 1){
NSLog(#"Running %# '%#'",self.class, NSStringFromSelector(_cmd));
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.checkedIndexPaths = [self initializationNSSet:self.checkedIndexPaths];
if ([self.checkedIndexPaths containsObject:indexPath])
{
[self.checkedIndexPaths removeObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[self.checkedIndexPaths addObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
The code works fine, when I use DetailButton and DetailDisclosureButton instead of Checkmark and None, but when I use checkmark and none the row of table view isn't clickable in neither case (the method ableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath is not called). How can I do to get changed Checkmark and None (maybe do clickable whole row). I need multiple rows with checkmark and also remove the checked row, when I click on it again.
If you don't have a disclosure button then tableView:accessoryButtonTappedForRowWithIndexPath: will never be called.
Instead, you would need to use tableView:didSelectRowAtIndexPath:.
Aside:
This code is never going to work usefully as if cell is nil you can't call a method on it:
if (cell == nil) {
cell.textLabel.textColor = [UIColor whiteColor];
}

How to pass an object when a button is clicked

I have a custom tableview cell which has a button. I need to pass the NSIndexPath object of tablview when the button is clicked. I am able to do is assign a tag to a button, receive the tag using sender..but I want to pass the NSIndexPath object...below is the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"shortCodeCell";
IPadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"KeywordsCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[IPadTableViewCell class]])
{
cell = (IPadTableViewCell *)currentObject;
}
}
}
// Delete
[cell.btnDelete addTarget:self action:#selector(onDelete:) forControlEvents:UIControlEventTouchUpInside];
cell.btnDelete.tag=indexPath.row;
return cell;
}
-(void)onDelete:(id)sender
{
UIButton *btn=(UIButton *)sender;
NSLog(#"BtnIndexpath to be deleted:%d",btn.tag);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
int errorCode = 0;
kd = [items objectAtIndex:btn.tag];
BOOL isDeleteKeyword= [ServerAPI deleteKeywordWithId:kd.keywordId :&errorCode];
dispatch_async (dispatch_get_main_queue (),
^{
if (isDeleteKeyword) {
[items removeObjectAtIndex:btn.tag];
//[tblKeywords deleteRowsAtIndexPaths:[NSArray arrayWithObject:btnIndexPath] withRowAnimation:YES];
[self.tblKeywords reloadData];
}
else return;
});
});
}
You can set the following in "cellForRowAtIndexPath"
cell.tag = indexPath.section;
cell.btnDelete.tag=indexPath.row;
And in "onDelete" you can create indexPath based on tags assigned.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:[[sender superview] tag]]
// I assume your UITableView called tableView
// Using this way you don't need to use tag
-(void)onDelete:(id)sender
{
UITableViewCell *cell = (UITableViewCell *)[sender superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}

putting limitation for checkbox in UITableView Objective-C

I create table programmatically with different sections and rows, i create check box inside table with pictures as a box
I want to put limitation for this check box
would you please help me
Edit :
my question is how can I choosing just one check box- limitation of choice
and also how can I unselect a box and remove the checkmark
here is code for check box row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
if (indexPath.section != 2) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text =contentForThisRow;
return cell;
}
else {
CheckBoxAbsenceTableViewCell *cell = (CheckBoxAbsenceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"CheckBoxAbsenceTableViewCell"];
cell.imageView.image=[UIImage imageNamed:#"emptycheck-box.png"];
cell.checkBox.image = image;
if(indexPath.row == 0){
cell.absenceCode.text =#"Red";
}
else if (indexPath.row == 1){
cell.absenceCode.text =#"Green";
}else if(indexPath.row == 2){
cell.absenceCode.text =#"Blue";
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckBoxAbsenceTableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.checkBox.image =[UIImage imageNamed:#"checkmark.png"];
}
Thanks in advance!
The easy solution is to add a UIInteger _selectedIndex instance var to your view controller.
In viewDidLoad set it to -1 (non of the rows is selected)
Then when user select a cell, save the selected index and reload the tableView:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
}
Change the cellForRowAtIndexPath method so it will select only the selected cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
if (indexPath.section != 2) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.textLabel.text =contentForThisRow;
return cell;
}
else {
CheckBoxAbsenceTableViewCell *cell = (CheckBoxAbsenceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"CheckBoxAbsenceTableViewCell"];
if (indexPath.row == _selectedIndex) {
cell.checkBox.image =[UIImage imageNamed:#"checkmark.png"];
}
else {
cell.checkBox.image = [UIImage imageNamed:#"emptycheck-box.png"];;
}
if(indexPath.row == 0){
cell.absenceCode.text =#"Red";
}
else if (indexPath.row == 1){
cell.absenceCode.text =#"Green";
}else if(indexPath.row == 2){
cell.absenceCode.text =#"Blue";
}
return cell;
}
}
From a UI design perspective, you should be using the cell.accessoryType and the UITableViewCellAccessoryCheckmark accessory.
You need to keep track of the selected (i.e. checked) indexPath in your data model. Lets call this self.idxPathSelected.
This didSelectRowAtIndexPath will add checkmark to the selected row, and clear checkmark from the previously selected row:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if (self.idxPathSelected != nil)
{
cell = [tableView cellForRowAtIndexPath:self.idxPathSelected];
cell.accessoryType = UITableViewAccessoryNone;
}
self.idxPathSelected = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
}
and in your cellForRowAtIndexPath, something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ((self.idxPathSelected != nil) && ([self.idxPathSelected compare:indexPath] == NSOrderedSame))
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
...
}

how to apply check mark on table view in iphone using objective c?

i m trying for apply for check mark in table view, but it is not working, if i checked again at that cell again then check mark apply. but not apply at new selected cell.
any one there who help me....
thanks aamir.
i m using following code
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section
{
return [self.chaptersList count];
}
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *CheckMarkCellIdentifier = #"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];
if ( cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [chaptersList objectAtIndex:row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
int row = [indexPath row];
int oldRow = [lastIndexPath row];
if (row != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Do you mean that the checkmark doesn't apply when you choose the first line at the first time? You can add an else sentence to the code, but it wastes some time when running the code.
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;
}
There is a funny bug here
if (row != oldRow)
because if oldRow was nil (or in another words... "0" ;-) and the user press the row = "0" the "if" sentence will think this row was checked.
I fixed like this:
- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
if (!self.lastIndexPath) {
self.lastIndexPath = indexPath;
}
if ([self.lastIndexPath row] != [indexPath row])
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
self.lastIndexPath = indexPath;
}
else {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
By the way... dont forget to use "self"