UITableView save selected items - objective-c

I need to save selected items for my UITableView in the NSMutableDictionary for iplementing checkboxes. So I do the following:
NSMutableDictionary * selDict;// - instance variable
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([selDict objectForKey:indexPath])
[selDict setObject:[NSNumber numberWithBool:FALSE] forKey:indexPath];
else
[selDict setObject:[NSNumber numberWithBool:TRUE] forKey:indexPath];
[[self tableView]reloadData];
}
- (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] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if ([selDict objectForKey:indexPath])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
...
return cell;
}
But it is only sets the items checked and does't work further.

You might want to change
if ([selDict objectForKey:indexPath])
to
if ([[selDict objectForKey:indexPath] boolValue])
What you are doing now is checking whether the object exists or not. It will work once when it is nil but not after that.

Related

Is it possible to load two screens of pictures on top and on the bottom at the same time as on the screen with UITableview cell

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

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

error with checkmark code

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

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

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