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;
}
Related
*My code
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ExploreTBLCellID";
ExploreTBLCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ExploreTBLCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"new one");
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
NSDictionary *distGetData;
distGetData = [arrget_smart_category objectAtIndex:indexPath.row];
[imgMain sd_setImageWithURL:[NSURL URLWithString:[distGetData objectForKey:#"image"]]
placeholderImage:imgPlaceholderExlore];
return cell;
}
In my cellForRow code I have the following and am getting an error saying nothing is being returned. What have I done wrong? Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
This is an endless loop: UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
This should be:
static NSString *reuseIdentifier = #"some_identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]
}
Your cellForRowAtIndexPath will look something like this (for the most Basic Table View)
- (UITableViewCell *) tableView :(UITableView *) tableView cellForRowAtIndexPath :(NSIndexPath *) indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier ";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex :row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
return cell;
}
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;
...
}
I m parsing json data and assigning it in my table view cell.where in the first row of tableview cell .i m assigning a static data(green text).if i scroll my tableview the data assigned in first table view cell is getting overlaped in my 6th and 7th tableview cell.below is the screen shotand the code.could u guys help me out
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
cell.textLabel.text=#"Pollenprognosen";
cell.textLabel.textColor=[UIColor greenColor];
cell.detailTextLabel.text=#"se dagens pollenprognos";
cell.detailTextLabel.font=[UIFont systemFontOfSize:([UIFont systemFontSize]-2)];
return cell;
}
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[story objectAtIndex:indexPath.row];
return cell;
}
}
I think it's because the cell is reused. Try this code instead:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
cell.textLabel.text=#"Pollenprognosen";
cell.textLabel.textColor=[UIColor greenColor];
cell.detailTextLabel.text=#"se dagens pollenprognos";
cell.detailTextLabel.font=[UIFont systemFontOfSize:([UIFont systemFontSize]-2)];
cell.accessoryType=UITableViewCellAccessoryNone;
return cell;
}
else
{
cell.textLabel.text=[story objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor blackColor];
cell.detailTextLabel.text=nil;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
Just make the else part as follows.
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[NSString stringWithFormat:#"Cell %d",indexPath.row];
cell.textLabel.textColor=[UIColor blackColor];
cell.detailTextLabel.text=#"";
return cell;
}
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];