Getting bad access while deleting cell and reloading table view - iphone

I am trying to build a table using custom UITableViewCell, but after deleting cell and when I try load the view, I get an EXC_BAD_ACCESS error,here i'm getting data from a dictionary and loading it to table view.
The code for my Table is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = (uploadCustomCell *)[tabelView1 dequeueReusableCellWithIdentifier:#"cell"];
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
if (cell == nil) {
[[NSBundle mainBundle]loadNibNamed:#"uploadCustomCell" owner:self options:nil];
cell = (uploadCustomCell *)self.uploadCustomcell;
}
saveBtnCcell.hidden = YES;
cell.textNamefield.hidden = YES;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
NSLog(#"dict dict :%#",dictionary);
//
cell.nameLabelCell.text = [dictionary valueForKey:#"VideoName"];
cell.userName.text = [dictionary valueForKey:#"User"];
NSString *defaultVideo = [dictionary valueForKey:#"DefaultVideo"];
if ([defaultVideo isEqualToString:#"1"]) {
[cell.defaultSwitch setOn:YES animated:YES];
}
else {
[cell.defaultSwitch setOn:NO animated:YES];
}
[cell.defaultSwitch addTarget:self action:#selector(setState:) forControlEvents:UIControlEventValueChanged];
cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
VideoNameTextField.hidden = YES;
return cell;
}
- (void)setState:(id)sender {
state = [sender isOn];
// NSString *rez = state == YES ? #"YES" : #"NO";
NSLog(#"state.........:%d",state);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"height:%f",uploadCustomcell.frame.size.height);
return 207;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *dictionary = [contents objectAtIndex:indexPath.row];
NSLog(#"Dictionary:%#",dictionary);
NSLog(#"indexpath:%#",indexPath);
//
NSLog(#"at index%d obj:%#",indexPath.row,dictionary);
NSString *nameDetails = [dictionary valueForKey:#"VideoName"];
guid = [dictionary valueForKey:#"GUID"];
detailsNameLbl.text = nameDetails;
detailsVehImg.image = [arrayimage objectAtIndex:indexPath.row];;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[contents removeObjectAtIndex:indexPath.row];
[tabelView1 reloadData];
}
}

Try this:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
You must use this method also:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[contents removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//[tabelView1 reloadData];
}

Related

assigning datasource to two tableviews in one view controller

Whats wrong in the following code?
here i am using two table views and assigning them with different data source, but its not working
-(void)viewDidLoad
{
[super viewDidLoad];
arryData = [[NSArray alloc] initWithObjects:#"MCA",#"MBA",#"BTech",#"MTech",nil];
arryData2 = [[NSArray alloc] initWithObjects:#"Objective C",#"C++",#"C#",#".net",nil];
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
btnOne.layer.cornerRadius=8;
btnTwo.layer.cornerRadius=8;
myTable1.layer.cornerRadius=8;
myTable2.layer.cornerRadius=8;
myTable1.delegate=self;
myTable1.dataSource=self;
myTable2.delegate=self;
myTable2.dataSource=self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1=tableView)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==self.myTable1)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
return cell;
}
else
{
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
return cell;
}
}
-(IBAction)btnCategory:(id)sender
{
if (flag==1) {
flag=0;
myTable1.hidden=NO;
myTable2.hidden=YES;
}
else{
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
myTable1.hidden=YES;
myTable2.hidden=YES;
}
-(IBAction)btnTopic:(id)sender
{
if (flag==1) {
flag=0;
myTable2.hidden=NO;
myTable1.hidden=YES;
}
else{
flag=1;
myTable2.hidden=YES;
myTable1.hidden=YES;
}
}
</code>
when i remove datasource of mytable2 all data gets added to table1 else nothing gets loaded in none of the table.
You did mistake. See my code..
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1==tableView) // ----- Change here you need to write == symbol instead of = symbol
{
return [arryData count];
} else {
return [arryData2 count];
}
}
Try this,
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==myTable1)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(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] ;
}
if(myTable1==tableView)
{
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
}
else
{
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
}
return cell;
}

Having issue with reading Plist into UITableView

I am trying to display a dictionary plist into UITableView , I have read many question and answer here but no luck ! it seems cells return null ! here is my plist :
and my codes :
-(void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"feed" ofType:#"plist"];
newsFeed = [NSArray arrayWithContentsOfFile:path];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return newsFeed.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dictionary.allKeys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
dictionary = [newsFeed objectAtIndex:indexPath.section];
cell.textLabel.text = [[newsFeed objectAtIndex:indexPath.row] valueForKey:#"title"];
return cell;
}
and the result is nothing :
Try this in you want to display only title then try this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return newsFeed.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
dictionary = [newsFeed objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary valueForKey:#"title"];
return cell;
}
EDIT
And if you want to display all record then try this
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return newsFeed.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dictionary.allKeys.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
dictionary = [newsFeed objectAtIndex:indexPath.section];
NSString *key =[[dictionary allKeys] objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary valueForKey:key];
return cell;
}
You should learn how to debug problems like this. Or if you tried, you should tell us what you tried in the question.
Set a breakpoint in viewDidLoad and make sure, the plist is loaded correctly. The path might be wrong. The file might be corrupt or have a different root object then what you're expecting.
Set a breakpoint in numberOfSectionsInTableView. Is it called at all? Is it called before or after you load your plist? Probably after. In that case you need to call [tableView reloadData]. If it's called what do you return.
Continue like this, step for step until you find the reason.
Try this,I use plist's a lot.
In ViewDidLoad
NSString *plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:self.plistName];
self.tableContents=[NSDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(#"table %#",self.tableContents);
NSLog(#"table with Keys %#",[self.tableContents allKeys]);
self.sortedKeys = [[self.tableContents allKeys] sortedArrayUsingSelector:#selector(localizedStandardCompare:)];
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
NSUInteger row = [indexPath row];
cell.textLabel.text = [NSString stringWithFormat:#" %#",[listData objectAtIndex:row]]; //take your object out
return cell;
}
Set the Root As NSDictionary, put your newsFeed in the dictionary.
Use NSDictionary *dict = [NSDictionary ...fromFile:...];
newsFeed = [dict valueForKey:#"some_key"];

Plist Search of Tableview

I have Plist which is been populated on the tableview with expanded sections ..now i want to search the table..below in images you can see what is happening when I search anything.
.
just because I am searching it but need some changes in the cellforrowatindexpath for search results....
please check the code and let me know what to do for searching plist..
what should be changes for the cellforrowatindexpath and noofrowsinsection for search from plist
.
.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.mySections count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows = 0;
if ([self tableView:tableView canCollapseSection:section] || !(tableView == self.searchDisplayController.searchResultsTableView) )
{
if ([expandedSections containsIndex:section] )
{
NSString *key = [self.mySections objectAtIndex:section];
NSArray *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];
return [dataInSection count];
}
return 1;
} else if(tableView == self.searchDisplayController.searchResultsTableView) {
rows = [self.searchResults count];
return rows;
}
return 1;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section {
NSString *key = [self.mySections objectAtIndex:section];
return [NSString stringWithFormat:#"%#", key];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//some changes required to display plst
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}
// Configure the cell...
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
}else {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [self.mySections objectAtIndex:section];
NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0];
NSArray *array=dataForSection.allKeys;
cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row];
cell.detailTextLabel.text=[dataForSection valueForKey:[array objectAtIndex:indexPath.row]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
// only first row toggles exapand/collapse
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
}
else
{
[expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
}
}
}
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"SELF contains[cd] %#",
searchText];
self.searchResults = [self.mySections filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
UISearchBar * searchBar = [controller searchBar];
[self filterContentForSearchText:searchString scope:[[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
UISearchBar * searchBar = [controller searchBar];
[self filterContentForSearchText:[searchBar text] scope:[[searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
The search results you are using in numberOfRows should be used for numberOfSections
Since you are filtering for section title and not rows.

How to perform Validation in ios uitableview (group table)?

In my app i have used UItableview group table.
In which there are two section and i want to perform validation on it.
for eg:In section 1 there are two row's,if user select row one user have to select some value from section 2 and if user have select second row in 1 section then no need of selection in section two.
following is my code:
- (void)viewDidLoad
{
NSArray *arr_data1 =[[NSArray alloc]initWithObjects:#"Yes",#"No",nil];
NSArray *arr_data2 =[[NSArray alloc] initWithObjects:#"Monotherapy",#"Adjunctive",nil];
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys:arr_data1,#"",arr_data2,
#"Was it monotherapy or adjunctive",nil];
self.tableContents =temp;
self.arr_data =[[self.tableContents allKeys]
sortedArrayUsingSelector:#selector(compare:)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.arr_data count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.arr_data objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSLog(#"fdfdfdf=%d",[indexPath row]);
NSString *rowValue = [listData objectAtIndex:row];
if ([rowValue isEqualToString:#"Yes"])
{
NSString *message = [[NSString alloc] initWithFormat:#"%#",rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You selected"
message:message delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
In - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,
you can check the section you need by using the following code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
//do as per you requirement
}
else if(indexPath.section == 1)
{
//do as per you requirement
}
}
You can first declare a variable count to keep track of how many items are selected in section 2 and isItemRequired as a flag variable and sythesize them, then use these delegates:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
A sample code to begin with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
isItemRequired = YES;
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"You must have to select one or more item in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
else
{
isItemRequired = NO;
}
}
else if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
{
count = 0;
for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
if(cell.selected==YES)
{
count++;
}
}
if(isItemRequired && count==0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Item Required" message:#"Atleast one item should be selected in section 2" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil ];
[alert show];
}
}
}
In the didselect method of UITableView, you needs to check the section number & then check which row is clicked from that section
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
//do this
}
else if (indexPath.row == 1)
{
//do this
}
}
else
{
//do this
}
}
In the following delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
add a check with the help of indexPath returned by the delegate, it will tell you that which section's which row is selected like :
if(indexPath.section == 0 && indexPath.row == 0)
{
// Select something from the another section also
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
// No need to select from another section.
}
First Take one BOOL variable with isYes in .h file like bellow...
BOOL isYes;
after in viewDidLoad: method just assign it to NO like bellow..
isYes = NO;
after that in didSelectRowAtIndexPath delegate method just use like bellow...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0 && indexPath.row == 0)
{
isYes = YES;
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
isYes = NO;
}
if(isYes){
if(indexPath.section == 1 )
{
//here user can select the section 2
}
}
}
i hope this help you....

iphone table view check mark accessory problem

I have a table with 50 rows. I want to select particular rows with checkmark accessory but when i select some rows and scroll down the table then i see pre checked rows. I know that table cell are reused but i want to emit this problem what can i do about this?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// return [array count];
return 50;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
You have to set cell.accessoryType in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The fact is that as you have a reuse identifier, when a new cell is requested, a cell with the identifier is copied. As you have set the checkmark in the latest cell, the checkmask is copied.
You have to store the state of each cell (ie in an array) to recreate the cell with the right value.
Something like that:
BOOL values[50]; //Not the best way, but easy for the example...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
values[indexPath.row] = !value[indexPath.row];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You can do it with the help of array ,take one array and each array hold a dict do this condition in array
NSMutableArray *arr = [[NSMutableArray alloc]init];
for(int i=1; i<=50;i++)
{
NSmutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setobject:[nsstring stringwithformat:#"%d",i] valueForkey:#"ID"];
[arr addobject:dict];
[dict release];
}
now rows is [arr count];
and put this check in cell appearance
NSMutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"]==nil])
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}else
{
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
now put below code in didselect methood
nsmutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
[dict setobject:[nsstring stringwithformate:#"2"] valueforkey:#"Check"];
}
else
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}