putting limitation for checkbox in UITableView Objective-C - 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;
...
}

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

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

Select only one cell UITableView

I have a table view with numerous cells. When I press one, a tick appears beside it, but when I select another, the tick on the previous one remains, and a new tick is added on the current cell. so two cells are ticked, but only one at a time must be ticked!!
I tried this, but it does not work:
if (cell.selected = YES) {
[cell setSelected:NO animated:YES];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}else if(cell.selected = NO){
[cell setSelected: YES animated:YES];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
- (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];
}
OR
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
An alternative:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (int i = 0; i<yourArray.count;i++)
{
if(i!=[indexPath row])
{
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
This physically goes through all of the rows and disables them. Kind of brute force, but it works. If you want a version that doesn't allow deselecting,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (int i = 0; i<yourArray.count;i++)
{
cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
The quick fix in Swift:
var lastIndexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let newRow = indexPath.row
let oldRow = lastIndexPath.row
if oldRow != newRow {
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
tableView.cellForRowAtIndexPath(lastIndexPath)?.accessoryType = .None
lastIndexPath = indexPath
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
In the tableviewdatasource you need to follow this:
- (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];
}
if ([indexPath compare:self.lastIndexPath] == NSOrderedSame)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
// UITableView Delegate Method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.lastIndexPath = indexPath;
[tableView reloadData];
}
the lastindex is property(retain) NSIndexPath* lastIndexPath;

iPhone's UITableViewCellAccessoryCheckMark

I have an issue in creating a CheckMarkAccessoryView in the UITableView.When i select a row in the table i can get a checked mark for that row,but randomly some other rows in the table also gets the check mark.I want only that cell to get the check mark.How can i do this?I am coding for an exclusive list.The following is the code which i have used.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==tableView1)
{
return […arraycount1…. ];
}
else
{
return […arraycount2…. ];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UIView* subview;
while ((subview = [[[cell contentView] subviews] lastObject]) != nil)
[subview removeFromSuperview];
}
if(tableView == tableView1)
{
cell.textLabel.text=[array1 objectAtIndex:indexPath.row];
}
else //Tableview2
{
cell.textLabel.text=[array2 objectAtIndex:indexPath.row];
}
cell.textLabel.font=[UIFont fontWithName:#"Georgia" size:20];
cell.textLabel.textAlignment=UITextAlignmentLeft;
[cell setSelectedBackgroundView:border];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastindex];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark);
{
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Kindly correct me where i am going wrong.
Thanks in advance for all the experts.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for (id algoPath in [tableView indexPathsForVisibleRows])
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:algoPath];
cell.accessoryType = UITableViewCellAccessoryNone;
if(algoPath == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
i tried different things out, nothing worked properly. now i'm iterating over all indices, setting all Accessories to None, and then the current one to accessoryCheckmark. not the cleanest approach but working.
UITableView reuses cell objects as you scroll the table, so setting the cell to show a checkmark means that it will also have a checkmark when it's reused
You need to store the state of each row in an array separate from the cells (or if you only need one cell at a time checked, just store an index somewhere), and then read from that array and set the appropriate accessoryType in the cellForRowAtIndexPath: method.
(alternatively you could simply disable cell reuse by removing the dequeueReusableCellWithIdentifier: call, but that's bad for performance if you have a long list to scroll)
The two if statement cancel each other, just add else like the following :
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastindex];
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
I think you are looking for these methods!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section!=0) {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
}
//didDeselect method in table view for removing previous checkmark
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section!=0)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
}

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