How do clickable checkmark accessory and none accessory? - objective-c

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];
}

Related

Tableview Checkmark not display using iOS storyboard with objective C

I am trying to create tableview with tableviewcell and whenever user select the tableviewcell It should apply checkmark. My tableview and tableview cell everything I have created by iOS storyboard. Please help step by step how to solve my issue.
My Source:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"cellID";
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[myTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSString *stringForCell;
if (indexPath.section == 0) {
stringForCell= [myData objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1){
stringForCell= [myData objectAtIndex:indexPath.row+ [myData count]];
}
[cell.sec_Label setText:stringForCell];
return cell;
}
You can do this way,first thing declare the variable's in header files
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"cellID";
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[myTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.tintColor = [UIColor blackColor];
NSString *stringForCell; // Mention in header file
if (indexPath.section == 0) {
stringForCell= [myData objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1){
stringForCell= [myData objectAtIndex:indexPath.row+ [myData count]];
}
[cell.sec_Label setText:stringForCell];
return cell;
}
write this in your tableview delegate function
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell ....//some creation
cell.accessoryType=UITableViewCellAccessoryCheckmark;
//cell.accessView don't set this, if set, use custom view. ignore accessoryType
return 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;
...
}

Issue with multiple UITableViewCellAccessoryCheckmark applied on single UITableCell touch

so I have a lot of cells in my UITableView and on touching a cell, additional cells that are just out of view also have ticks applied. I believe this is because the cells are being reused or something due to dequeueReusableCellWithIdentifier:
Heres my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone){
[addPapersSelectedRowsArray addObject:[NSNumber numberWithInt:indexPath.row]];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
cell.accessoryType = UITableViewCellAccessoryNone;
[addPapersSelectedRowsArray removeObject:[NSNumber numberWithInt:indexPath.row]];
}
}
- (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];
}
NSDictionary *currentPapersSecltion = [papersDataDictionary objectAtIndex:indexPath.row];
[[cell textLabel] setText:[currentPapersSecltion objectForKey:#"Title"]];
return cell;
}
Also pulling data from a .plist in viewDidLoad:
//Load papers data from the local plist
papersDataDictionary = [[[NSDictionary alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"papers.plist"]] objectForKey:#"Rows"];
Does anyone know how to fix this?
Thanks guys!
Dex
In cellForRowAtIndexPath which you need to set your checkmark according to whether or not the cell has been selected.
if ([addPapersSelectedRowsArray containsObject:[NSNumber numberWithInt:[indexPath row]]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// add this line
cell.accessoryType = UITableViewCellAccessoryNone;
if (cell.accessoryType == UITableViewCellAccessoryNone){
[addPapersSelectedRowsArray addObject:[NSNumber numberWithInt:indexPath.row]];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
cell.accessoryType = UITableViewCellAccessoryNone;
[addPapersSelectedRowsArray removeObject:[NSNumber numberWithInt:indexPath.row]];
}
//add this line as well
[tableView reloadData];

How to remove a button from UITableViewCell?

I have customized a UITableViewCell in that I have a UILabel and UIButton. I have set the frame in layoutSubviews method now that table has customized UITableViewCell that I am using in three places, but I need to remove UIButton in second place. How to do that ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomPopTableViewCell *cell = (CustomPopTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[CustomPopTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if(tableView.tag == e_metadataView)
{
//cell.mCheakMarkButton.frame=CGRectZero;
cell.mTaggedTerm.text = [docInfoCollection objectAtIndex:indexPath.row];
}
else if(tableView.tag == e_searchSettingView)
{
if(indexPath.row == self.currentRow)
{
[cell.mcheckMarkButton removeFromSuperView];
}
cell.mTaggedTerm.text = [searchEngineCollection objectAtIndex:indexPath.row];
}
else if(tableView.tag == e_TaggedTermView)//tageed term table
{
TaggedItems *taggedItem = [documentData.taggedWords objectAtIndex : indexPath.row];
cell.mTaggedTerm.text = taggedItem.keyword;
if([self isTappedObjectExist:indexPath.row])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
return cell;
}
I would use :
for (UIView* v in cell.subviews) {
if ([v isKindOfClass:[UIButton class]]) {
[v removeFromSuperView];
}
}
To be amended if you have an intermediary level of view in your custom cell of course. If you have several buttons I would use a tag to identify it.

UITableView Checklist

I'm making a checklist program using UITableView and I managed to implement everything up to selection and making a checkmark appear. However, when I scroll away from the cells. The checkmark disappears.
I have the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
if(tableView == Table1){
switch(indexPath.section){
case 0:
cell.textLabel.text =[array1 objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = [array3 objectAtIndex:indexPath.row];
break;
}
//cell.textLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
}
//if([indexPath compare:[selectedArray objectAtIndex:i]] == NSOrderedSame){
if([selectedArray containsObject:cell]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
//}
if(tableView == sauceTable){
cell.textLabel.text = [array4 objectAtIndex:indexPath.row];
}
return cell;}
And:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *c= [aTableView cellForRowAtIndexPath: indexPath];
c.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedArray addObject: c];
}
Any ideas?
Solved. Used NSIndexPath instead of UITableViewCell for the selectionArray