Replacing one NSMutableDictionary object in NSMutableArray, replaces all objects - ios

I am fairly knew to iOS development and I think I jumped into the deep end too fast.
I am trying to replace a NSMutableDictionary inside an NSMutableArray of dictionaries by using replaceObjectAtIndex:withObject: but when I reload my tableView, all the objects in the array have been replaced by the one I am trying to replace,not just the specific index.
Thanks in advance for your help.
- (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];
}
if ((self.editing && indexPath.row == [detailsArray count])) {
cell.textLabel.text = #"Add Detail";
return cell;
}
[cell.textLabel setText:[[detailsArray objectAtIndex:indexPath.row] objectForKey:#"name"]];
[cell.detailTextLabel setText:[[detailsArray objectAtIndex:indexPath.row] objectForKey:#"desc"]];
return cell;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
if (!editing) {
[dictionaryList setValue:name.text forKey:#"name"];
[dictionaryList setValue:description.text forKey:#"desc"];
[detailsArray replaceObjectAtIndex:IndexHelper.row withObject:dictionaryList];
}
[self.tableView reloadData];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
if ([detailsArray count] > 0) {
[detailsArray removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
[dictionaryList setValue:#"Title" forKey:#"name"];
[dictionaryList setValue:#"Details" forKey:#"desc"];
[detailsArray addObject:dictionaryList];
[[self tableView] endUpdates];
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[[self tableView] endUpdates];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
IndexHelper = indexPath;
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"" message:#"\n\n\n\n"
delegate:self cancelButtonTitle:#"Άκυρο" otherButtonTitles:#"Αποθήκευση", nil];
UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"passwordfield" ofType:#"png"]]];
passwordImage.frame = CGRectMake(11,20,262,33);
[passwordAlert addSubview:passwordImage];
name = [[UITextField alloc] initWithFrame:CGRectMake(16,25,252,21)];
name.font = [UIFont systemFontOfSize:18];
name.backgroundColor = [UIColor whiteColor];
name.secureTextEntry = NO;
name.keyboardAppearance = UIKeyboardAppearanceAlert;
name.delegate = self;
name.placeholder = #"Title";
[name becomeFirstResponder];
[passwordAlert addSubview:name];
UIImageView *passwordImage2 = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"passwordfield" ofType:#"png"]]];
passwordImage2.frame = CGRectMake(11,65,262,33);
[passwordAlert addSubview:passwordImage2];
description = [[UITextField alloc] initWithFrame:CGRectMake(16,70,252,21)];
description.font = [UIFont systemFontOfSize:18];
description.backgroundColor = [UIColor whiteColor];
description.secureTextEntry = NO;
description.keyboardAppearance = UIKeyboardAppearanceAlert;
description.delegate = self;
description.placeholder = #"Details";
[description becomeFirstResponder];
[passwordAlert addSubview:description];
[passwordAlert show];
}
---This is where I am replacing the dictionary in the array---
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (buttonIndex==1) {
if ([name.text isEqualToString:#""] && [description.text isEqualToString:#""]) {
return;
}
[dictionaryList setValue:name.text forKey:#"name"];
[dictionaryList setValue:description.text forKey:#"desc"];
[detailsArray replaceObjectAtIndex:indexPath.row withObject:dictionaryList];
}
[self.tableView reloadData];
NSLog(#"%#",detailsArray);
}

I think you are adding same dictionary at all the indexes to it takes a common reference you have to check and make multiple instances for the dictionary for each index.
It may help you

I think the problem lies in the first of the code below
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; //REMOVE THIS LINE
NSIndexPath *indexPath = IndexHelper; //USE THIS LINE
if (buttonIndex==1) {
if ([name.text isEqualToString:#""] && [description.text isEqualToString:#""]) {
return;
}
[dictionaryList setValue:name.text forKey:#"name"];
[dictionaryList setValue:description.text forKey:#"desc"];
[detailsArray replaceObjectAtIndex:indexPath.row withObject:dictionaryList];
}
[self.tableView reloadData];
NSLog(#"%#",detailsArray);
}

Related

Buggy swipe to delete display on one more row after each delete

I'm having a weird issue on UITableView delete action since iOS 11.
Here's the relevant TableView code :
#implementation ChatMessageListViewController(TableView)
#pragma mark - table view datasource/delegate
- (NSArray<UITableViewRowAction *> *) tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
NSMutableArray *rowActions = [NSMutableArray array];
UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:#"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
[self deleteMessageAtIndexPath:indexPath];
}];
delete.backgroundColor = [UIColor redColor];
[rowActions addObject:delete];
return [rowActions copy];
}
- (void) deleteMessageAtIndexPath:(NSIndexPath *)indexPath {
NSString *threadID = [[self.messageArray objectAtIndex:indexPath.row] objectForKey:#"threadID"];
[self.tableView beginUpdates];
[self.messageArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
#weakify(self);
[UIUtil showLoadingHudWithText:WELocalString(#"message_remove_thread_loading_text", #"Deleting...", #"删除中...")];
[[AsyncUtil sharedInstance] dispatch_background_network:^{
DBManager *db = [[DBManager alloc] init];
[db deletetableData:[NSString stringWithFormat:#"singleChat WHERE threadID = '%#' ",threadID] ];
[[MemChatThreadMessages sharedInstance] removeThread:threadID];
NSDictionary * result = [Network deleteChatThread:threadID forEmail:[WEUtil getEmail]];
[[AsyncUtil sharedInstance] dispatch_main:^{
[UIUtil hideLoadingHuds];
#strongify(self);
if(self == nil) return ;
if([result[#"result"] isEqualToString:#"success"]){
}else{
[UIUtil showErrorMessage:WELocalString(#"message_remove_thread_error", #"Cannot delete this thread", #"不能删除该会话!")];
}
[self.tableView reloadData];
}];
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.messageArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *messageInfo = [self.messageArray objectAtIndex:indexPath.row];
if ([(NSString *)[messageInfo objectForKey:#"isAnnouncement"] isEqualToString:#"1"]) {
return 80;
}else if ([[messageInfo objectForKey:#"chatTag"] isValidString]){
return 80;
}else if([self isSpecialMessage:messageInfo]){
return 80;
}else{
return 67;
}
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"message";
if(self.events == nil){
NSDictionary * d = [WEUtil getMyEventListCache];
self.events = [[NSMutableDictionary alloc] init];
for(NSDictionary * eventSummary in d[#"events"]){
NSString * eventID = eventSummary[#"eventid"];
[self.events setObject:eventSummary forKey:eventID];
}
}
UserMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UserMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if(indexPath.row >= [self.messageArray count]){
TERMINATE_WITH_NIL_CELL;
}
NSDictionary *messageInfo = [self.messageArray objectAtIndex:indexPath.row];
if(![self isSpecialMessage:messageInfo]){
[cell configureCellWithMessageDict:messageInfo];
}else{
[cell configureCellWithNewMessageDict:messageInfo withEvents:self.events];
}
return cell;
}
#pragma mark - Navigation
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *msgThreadDict = [self.messageArray objectAtIndex:indexPath.row];
if(![self isSpecialMessage:msgThreadDict]){
[self tableView:tableView didSelectNormalRowAtIndexPath:indexPath];
}else{
NSString * event = msgThreadDict[#"event"];
if([event isValidString]){
if([event isEqualToString:#"no_event_messages"]){
[UIUtil showErrorMessage:#"no event id"];
}else{
[BackendTracking trackingWithAction:#"open_special" withLabel:#"threads_list"];
SpecialTopicListViewController * special = [[SpecialTopicListViewController alloc] init];
special.tracking_src = #"tab";
[self.navigationController pushViewController:special animated:YES];
}
}
}
}
-(void) tableView:(UITableView *)tableView didSelectNormalRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *msgThreadDict = [self.messageArray objectAtIndex:indexPath.row];
NSString *threadID = [msgThreadDict objectForKey:#"threadID"];
NSString *jid = [msgThreadDict objectForKey:#"jid"];
[GATracking trackCategory:#"message" withAction:#"thread_list_item_click" withLabel:threadID];
[[MemChatThreadMessages sharedInstance] setCurrentThreadID:threadID];
PrivateMessageViewController * chatVC = [[PrivateMessageViewController alloc] init];
chatVC.threadID = threadID;
chatVC.targetJID = jid;
chatVC.targetName = [msgThreadDict objectForKey:#"name"];
chatVC.unreadMsgNumber = [[self.messageArray objectAtIndex:indexPath.row][#"unreadCnt"] integerValue];
if ([(NSString *)[msgThreadDict objectForKey:#"isGroup"] isEqualToString:#"1"]) {
chatVC.isGroup = YES;
}else{
chatVC.isGroup = NO;
}
chatVC.src = #"list";
WELogInfo(#"click message");
[self.navigationController pushViewController:chatVC animated:YES];
}
#end
With the update and the changes using those trailing swipe actions there is another View appended before each time I delete an entry (until it doesn't work anymore). I've tried disabling the full trail or implementing iOS 11 trailingSwipeActionsConfigurationForRowAtIndexPath but I can't resolve this issue so far.
Do you see something wrong in the code? The main controller code is in another file.
Try reloading after you delete, after this line
[self.tableView endUpdates];
I think you removed the data from messageArray but as you are not reloading just after that so table view count is still 2 and you are reloading inside the block which might be taking time.
And one more thing you already removing data from messageArray, and then removing from db, So if you fail to remove it from db you are showing its not removed but for user it will be removed, as its no longer in message array

How to pass value in select UITableViewCell Row

I have UITableViewCell, cell indexing category wise and expand cell click on header, and load data by JSON. I want to select cell and pass data another view controller. I try to many times but pass data only first cell. I clicked another cell so this condition pass data first cell. Can't send particular cell data. How it possible . please help.
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSArray *statuses = [json objectForKey:#"status"];
names=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:#"business_category_name"]];
business_details_array=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:#"business_details"]];
for (int i=0; i<[names count]; i++) {
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
}
[self.tabel_view reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return names.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
return [[business_details_array objectAtIndex:section] count];
}
else
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 70;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,70)];
sectionHeaderView.backgroundColor=[UIColor grayColor];
sectionHeaderView.tag=section;
UIView *sectionsubHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,60)];
sectionsubHeaderView.backgroundColor=[UIColor blueColor];
UIImageView *arrow=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,60, 60)];
[arrow setImage:[UIImage imageNamed:#"arrow.png"]];
UILabel *Lbl=[[UILabel alloc]initWithFrame:CGRectMake(60, 0,tableView.frame.size.width-60, 60)];
Lbl.text=[names objectAtIndex:section];
Lbl.textColor=[UIColor whiteColor];
[sectionsubHeaderView addSubview:arrow];
[sectionsubHeaderView addSubview:Lbl];
[sectionHeaderView addSubview:sectionsubHeaderView];
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(sectionHeaderTapped:)];
[sectionHeaderView addGestureRecognizer:headerTapped];
return sectionHeaderView;
}
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
for (int i=0; i<[names count]; i++) {
if (indexPath.section==i) {
[arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
}
}
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ht"];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString*title_str=[NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"name"] objectAtIndex:indexPath.row]];
cell.title.text= title_str;
[titles addObject:title_str];
NSLog(#"get %#",titles);
cell.email.text=[NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"email"] objectAtIndex:indexPath.row]];
//[emailary addObject:cell.email.text];
cell.address_lbl.text=[NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"address"] objectAtIndex:indexPath.row]];
//[adrsary addObject:cell.address_lbl.text];
cell.phone_lbl.text=[NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"phone"] objectAtIndex:indexPath.row]];
// [phoneary addObject:cell.phone_lbl.text];
cell.web_lbl.text=[NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"website"] objectAtIndex:indexPath.row]];
//[websiteary addObject:cell.web_lbl.text];
cell.sens_lbl.text=[NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"member_since"] objectAtIndex:indexPath.row]];
//[sensary addObject:cell.sens_lbl.text];
cell.des_lbl.text=[NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"des"] objectAtIndex:indexPath.row]];
//[desary addObject:cell.des_lbl.text];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"%#",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:#"img_url"] objectAtIndex:indexPath.row]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.image_view.image =image;
//[images addObject:cell.image_view.image];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showRecipeDetail"])
{
NSIndexPath *indexPath = [self.tabel_view indexPathForSelectedRow];
member_details *destViewController = segue.destinationViewController;
destViewController.hello = [titles objectAtIndex:indexPath.row];
}
}
As you have directly joined segue from tableview, it's not proper way to do that. Instead of you have to joined segue by controller to controller. So before getting proper selected row index, your view controller push fast and you are always getting 0 index.
Instead of writing logic in prepareForSegue, you can do the same stuff in didSelect method of UITablView.
For that you have to find your destination view controller from storyboard. And set the data to required property.
EDIT
Remove your prepare segure logic and do this in didSelect
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:[NSBundle mainBundle]];
YourDestinationVC *vc =[[storyboard instantiateViewControllerWithIdentifier:#"YourDestinationVC"]; //#"YourDestinationVC" is storyboardID for your destination view controller
vc.hello = [titles objectAtIndex:indexPath.row];
[[self navigationController] pushViewController:vc animated:YES];
}
Here is screenshot :
You should create your segues between viewcontrollers.
Do not create segue from cell to viewcontrollers.
Then you should call
[self performSegueWithIdentifier:#"showRecipeDetail" sender:self];
after updating the table in didSelectRowAtIndexPath
Try this remove prepare for prepareForSegue and in didselectrowatindexpath add.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
member_details *destViewController = [[member_details alloc]init];
destViewController.hello = [titles objectAtIndex:indexPath.row];
[[self navigationController] pushViewController:destViewController animated:YES];
}
Try This
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"showRecipeDetail" sender:[YOUR_ARRAY objectAtIndex:indexPath.row]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showRecipeDetail"])
{
member_details *destViewController = segue.destinationViewController;
destViewController.hello = sender;
}
}

Objective-C: Change UIImageView in Custom Cell change different cell images

I am using a custom cell on UITableView with multi Section and i’m added a UITapGestureRecognizer to a UIImageView for change it when the image is selected but When I select the image other's images in different cell are selected as well.
This is the code for the TableView datasource method cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FilterCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UILabel *text = (UILabel *)[cell viewWithTag:ROW_TITLE];
UIImageView *image = (UIImageView *)[cell viewWithTag:ROW_IMAGE];
NSDictionary *item = [results objectAtIndex:indexPath.section];
NSString *string = [item objectForKey:JSONResp_common_name];
[text setText:((string != [NSNull null])? string : #"")];
if ([[item objectForKey:JSONResp_common_status] isEqualToNumber:[NSNumber numberWithBool:YES]])
[image setImage:[UIImage imageNamed:#"checked"]];
else
[image setImage:[UIImage imageNamed:#"unchecked"]];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
[image setTag:indexPath.row];
[image setUserInteractionEnabled:YES]; action:#selector(handleTapFrom:)];
if([self tableView:tableView
canCollapseSection:indexPath.section]){ // EXPANDABLE ROW
if(!indexPath.row){// HEADER
// SET ACCESORIES
if([expandedSections containsIndex:indexPath.section])
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menos"]]];
else
[cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mas"]]];
[image setUserInteractionEnabled:NO];
//Remove checked option from a cell
for(UITapGestureRecognizer *tapGesture in cell.gestureRecognizers)
[cell removeGestureRecognizer:tapGesture];
}else{// SUB ROWS
NSArray *subValues = [item objectForKey:PARAM_STRUCT_SUBELEMENTS];
NSDictionary *detail = [subValues objectAtIndex:indexPath.row -1];
//REASING VALUES TO SUB VALUES
if(detail && [detail isKindOfClass:[City class]]){
City *currentCity = (City *)detail;
[text setText:[currentCity name]];
}
[cell setAccessoryView:nil];
[tapGestureRecognizer setDelegate:self];
[cell addGestureRecognizer:tapGestureRecognizer];
}
}else{//ROWS CHECKED OPTION
[cell setAccessoryView:nil];
[tapGestureRecognizer setDelegate:self];
[cell addGestureRecognizer:tapGestureRecognizer];
}
return cell;
}
</code>
</pre>
This is the code for the TableView datasource method didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *data = [self.results objectAtIndex:indexPath.section];
if([self tableView:tableView
canCollapseSection:indexPath.section]){
if(!indexPath.row){// EXPANDABLE
// only first row toggles expandaed/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 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mas"]];
}else{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"menos"]];
}
}else{// Detail
}
}else{ // Normal Row
//[]
}}
This is the code for the TableView datasource method fro the UITapGestureRecognizer
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
UITableViewCell *cellView = (UITableViewCell *)[recognizer view];
UIImageView *switchImageView = nil;
for (UIView *item in [[[cellView subviews] firstObject] subviews]) {
if ([item isKindOfClass:[UIImageView class]] && ((UIImageView *)item).image != nil) {
switchImageView = (UIImageView *)item;
break;
}
}
if (switchImageView == nil) {
for (UIView *item in [[[[[cellView subviews] firstObject] subviews] objectAtIndex:1] subviews]) {
if ([item isKindOfClass:[UIImageView class]] && ((UIImageView *)item).image != nil) {
switchImageView = (UIImageView *)item;
break;
}
}
}
NSDictionary *item = [results objectAtIndex:switchImageView.tag];
if ([Utility image:switchImageView.image
isEqualTo:[UIImage imageNamed:IMAGE_UNCHECKED]]) {
if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_sortings]) {
[delegate didCheckFilter:item
withStatus:YES];
} else if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_filters]) {
[switchImageView setImage:[UIImage imageNamed:IMAGE_CHECKED]]; //change to a selected image
[delegate didCheckFilter:[item objectForKey:JSONResp_common_type]
withStatus:YES];
}
} else {
if ([[item objectForKey:JSONResp_common_action] isEqualToString:JSONResp_filters]) {
[switchImageView setImage:[UIImage imageNamed:IMAGE_UNCHECKED]];
[delegate didCheckFilter:[item objectForKey:JSONResp_common_type]
withStatus:NO];
}
}}
While declaring the UITapGestureRecognizer on particular cell assign the tag as
cell.tag = indexPath.row;
[cell addGestureRecognizer:tapGestureRecognizer];
and in your handler do like this
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
UITableViewCell *cellView = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:recognizer.tag inSection:0]];
// your stuff
}
Hope this Helps!

Get values from a plist file in iOS

I am creating a Plist file as shown below
I want to list all the Items where level is 1 and I can only use accessoryType = UITableViewCellAccessoryCheckmark if level is 1. How can I do it.
I am loading my plist file here:
- (void)viewDidLoad
{
[super viewDidLoad];
countId = 0;
NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Topic" ofType:#"plist"]];
self.items=[dict valueForKey:#"Items"];
self.itemsInTable=[[NSMutableArray alloc] init];
[self.itemsInTable addObjectsFromArray:self.items];
[self.menuTableView registerNib:[UINib nibWithNibName:NSStringFromClass([IndicatorTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([IndicatorTableViewCell class])];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStylePlain
target:self
action:#selector(doneSelection:)];
[self.navigationItem setRightBarButtonItem:myButton];
}
My code for cellForRowAtIndexpath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *Title= [[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:#"Name"];
return [self createCellWithTitle:Title image:[self.itemsInTable objectAtIndex:indexPath.row] indexPath:indexPath];
}
My code for didSelectRowAtIndexPath is :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dic=[self.itemsInTable objectAtIndex:indexPath.row];
if([dic valueForKey:#"SubItems"])
{
NSArray *arr=[dic valueForKey:#"SubItems"];
BOOL isTableExpanded=NO;
for(NSDictionary *subitems in arr )
{
NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
isTableExpanded=(index>0 && index!=NSIntegerMax);
if(isTableExpanded) break;
}
if(isTableExpanded)
{
[self CollapseRows:arr];
}
else
{
NSUInteger count=indexPath.row+1;
NSMutableArray *arrCells=[NSMutableArray array];
for(NSDictionary *dInner in arr )
{
[arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
[self.itemsInTable insertObject:dInner atIndex:count++];
}
[self.menuTableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationLeft];
}
}
if([[[dic valueForKey:#"SubItems"]objectAtIndex:0]objectForKey:#"level"])
{
// NSArray *arr=[dic valueForKey:#"SubItems"];
// if ([[arr objectAtIndex:0 ] intValue] == #"1")
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
if(countId <5)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
countId = countId + 1;
}
else
{
UIAlertView *dialog;
dialog =[[UIAlertView alloc] initWithTitle:#"Alert Message"
message:#"Select maximum 5 countries"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[dialog show];
// NSLog(#"Greater then 5");
}
}
else
{
if(countId>0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
countId--;
}
else
{
//show alert
UIAlertView *dialog;
dialog =[[UIAlertView alloc] initWithTitle:#"Alert Message"
message:#"Select atleast 1 country"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
[dialog show];
// NSLog(#"must choose 1");
}
}
// countId = [self.tableView indexPathsForSelectedRows].count;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
}
Please do reply. I am stuck here
To check level = 1 to add accessoryType = UITableViewCellAccessoryCheckmark try
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *itemDictionary = [self.itemsInTable objectAtIndex:indexPath.row];
NSArray *subItems = [itemDictionary valueForKey:#"SubItems"];
NSDictionary *firstItem = subItems[0];
if ([[firstItem objectForKey:#"level"] integerValue] == 1) {
//Set appropriate accessory view here
} else {
//Check the cell accessory type and update this too
//This is to avoid wrong accessory view on cell reuse
}
}
You can use the following code to get the root object, then go from there:
NSDictionary *rootDict = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"Root"];

Select All,Deselect All and select particular cell in custom table view

I have created a drop-down using table cell,where i am showing my data. My data is get display in drop down.Now I want all the cell should be selected initially. Also I want to deselect all cell, and individual cell.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView isEqual:expansionTableView]) {
forState:UIControlStateNormal];
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
objectForKey:#"surveyName"];
NSMutableArray *list=[[NSMutableArray alloc]init];
NSMutableArray *idlist =[[NSMutableArray alloc]init];
for (int i=0; i<_arraySurveyName.count; i++) {
NSMutableDictionary *dict=[_arraySurveyName objectAtIndex:i];
NSString *surveyName=[dict valueForKey:#"SurveyName"];
NSString *surveyID =[dict valueForKey:#"SurveyId"];
[list addObject:surveyName];
[idlist addObject:surveyID];
}
NSString *item = [list objectAtIndex:indexPath.row-1];
NSNumber *item1= [idlist objectAtIndex:indexPath.row-1];
str = item1;
NSLog(#"%#",str);
[_btn_surveyName setTitle:item forState:UIControlStateNormal];
[expansionTableView setHidden:YES];
}
}
else if ([tableView isEqual:expansionTableViewQtn]){
NSString *selectedQuestion=[arrayOfQuestionDetail objectAtIndex:indexPath.section];
[expansionTableViewQtn setHidden:YES];
[_btn_showQuestn setTitle:selectedQuestion forState:UIControlStateNormal];
}
}
it's my code for "didSelect".How to do this.
You can use below methods to select and deselect the cells
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
[tableView deselectRowAtIndexPath:indexPath animated:NO];//For deselecting
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];//For selecting

Resources