UIsegmented control not working inside collection view - ios

Hi I am trying to trigger events for UIsegment control inside the collection view.
here is my code.
CollectionViewCell.h
#property (strong, nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
ViewController.m
{
NSInteger selectedSegment;
}
- (UIView *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
cell.mySegmentedControl.tag = indexPath.row;
selectedSegment = cell.mySegmentedControl.selectedSegmentIndex;
[cell.mySegmentedControl addTarget:self action:#selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void) segmentValueChanged: (UISwitch *) sender {
//NSInteger index = sender.tag;
if(selectedSegment == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"!Alert"
message:#"Do you think this property is not exists?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Yes", nil];
[alert show];
}
else
{
//your code
}
}
The above code not works for me.Any help will be appreciated.

is not UISwitch it is UISegmentedControl do like
- (void) segmentValueChanged: (UISegmentedControl *) sender {
//NSInteger index = sender.tag;
if(sender.selectedSegmentIndex == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"!Alert"
message:#"Do you think this property is not exists?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Yes", nil];
[alert show];
}
else
{
//your code
}
}

Related

Saving data of a table view cell

I have 3 textfields, a submit button and a table view in a view controller. Now I want to show the datas of the 3 text fields to 3 labels of a custom cell in my table view and when I again update the datas the updated datas should be shown in the next cell. But in my code the updated datas are showing in the cells. The previous datas are overwriting. SO can any one tell me where am I wrong?
This is my ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *nameTextField;
#property (weak, nonatomic) IBOutlet UITextField *addressTextField;
#property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
- (IBAction)submitButton:(id)sender;
#property (weak, nonatomic) IBOutlet UITableView *detailTable;
#property(nonatomic, strong)NSMutableArray *arrPeopleDetail;
#end
This is my ViewController.m
#import "ViewController.h"
#import "detailObject.h"
#interface ViewController ()
#end
#implementation ViewController
{
detailObject *peopleDetail;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arrPeopleDetail = [[NSMutableArray alloc]init];
peopleDetail = [[detailObject alloc]init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)submitButton:(id)sender {
[self.detailTable reloadData];
if ([self.nameTextField.text isEqualToString:#""] || [self.addressTextField.text isEqualToString:#""] || [self.phoneTextField.text isEqualToString:#""] ) {
UIAlertView *alrt = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Plaeas Enter Text In The Empty Field" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil ];
[alrt show];
}
else{
peopleDetail.strPeopleName = self.nameTextField.text;
peopleDetail.strPeopleAddress = self.addressTextField.text;
peopleDetail.strPeoplePhoneNumber = self.phoneTextField.text;
[self.arrPeopleDetail addObject:peopleDetail];
}
//[self.detailTable reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell1"];
UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:#"strPeopleName"];
UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:2];
lbl2.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:#"strPeopleAddress"];
UILabel *lbl3 = (UILabel*)[cell.contentView viewWithTag:3];
lbl3.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:#"strPeoplePhoneNumber"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrPeopleDetail count];
}
#end
This is the .h NSObject class that I have Taken.
#import <Foundation/Foundation.h>
#interface detailObject : NSObject
#property(nonatomic,strong) NSString *strPeopleName;
#property(nonatomic,strong) NSString *strPeopleAddress;
#property(nonatomic,strong) NSString *strPeoplePhoneNumber;
#end
Add line peopleDetail = [[detailObject alloc]init]; in else case of submitButton action and after adding the new object into array reload table view data
i.e.
else{
peopleDetail = [[detailObject alloc]init];
peopleDetail.strPeopleName = self.nameTextField.text;
peopleDetail.strPeopleAddress = self.addressTextField.text;
peopleDetail.strPeoplePhoneNumber = self.phoneTextField.text;
[self.arrPeopleDetail addObject:peopleDetail];
[self.detailTable reloadData];
}
You can remove the peopleDetail = [[detailObject alloc]init]; from viewDidLoad
I realised where I was wrong. I was creating the object globally instead of creating it locally. If anyone does this mistake what happens is, every time when user enters data the datas are being overwritten because the created object is one. So the main mistake was creating the object globally instead of creating it locally.
So the updated ViewController.m is..
#import "ViewController.h"
#import "detailObject.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arrPeopleDetail = [[NSMutableArray alloc]init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)submitButton:(id)sender {
if ([self.phoneTextField.text isEqualToString:#""] && [self.addressTextField.text isEqualToString:#""] && [self.phoneTextField.text isEqualToString:#""] ) {
UIAlertView *alrt = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Please Fillup The Above Fields" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alrt show];
}
else if ([self.nameTextField.text isEqualToString:#""]) {
UIAlertView *alrt_name = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Plaeas Enter Name" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil ];
[alrt_name show];
}
else if ([self.addressTextField.text isEqualToString:#""]) {
UIAlertView *alrt_address = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Please Enter Address" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alrt_address show];
}
else if ([self.phoneTextField.text isEqualToString:#""]) {
UIAlertView *alrt_phoneNumber = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Please Enter Phone Number" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alrt_phoneNumber show];
}
else
{
detailObject *peopleDetail = [[detailObject alloc] init];
peopleDetail.strPeopleName = self.nameTextField.text;
peopleDetail.strPeopleAddress = self.addressTextField.text;
peopleDetail.strPeoplePhoneNumber = self.phoneTextField.text;
[self.arrPeopleDetail addObject:peopleDetail];
self.nameTextField.text = #"";
self.addressTextField.text = #"";
self.phoneTextField.text = #"";
}
[self.detailTable reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell1"];
UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:#"strPeopleName"];
UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:2];
lbl2.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:#"strPeopleAddress"];
UILabel *lbl3 = (UILabel*)[cell.contentView viewWithTag:3];
lbl3.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:#"strPeoplePhoneNumber"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrPeopleDetail count];
}
#end

Display an alert on Swipe Delete in iOS

I'm a beginner in iOS. I'm performing a swipe delete option. I want to display an alert view before deleting the row. How can i perform this action.
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",collisionsArray);
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
NSString *userId = [userinfo valueForKey:#"user_id"];
if(userId!=nil)
{
NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
collisionId = [NSString stringWithFormat:#"%#",[dict valueForKey:#"collisionId"]];
NSLog(#"%#",collisionId);
// removes saved datas from database
BOOL result = [database removeCollisionDetails:collisionId:#"accident_report"];
if(result)
{
[[SHKActivityIndicator currentIndicator]
displayCompleted:NSLocalizedString(#"val_sucess_vehicle", nil)];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[[SHKActivityIndicator currentIndicator]
displayCompleted:NSLocalizedString(#"val_error", nil)];
}
}
}
[self.tableView reloadData];
}
For this you can just display an alet view in:
if (editingStyle == UITableViewCellEditingStyleDelete){
// Show your alert view
// Set its delegate to self
}
Now you have to do something like:
#pragma mark ---- Delegate for alertview ----
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
NSString *userId = [userinfo valueForKey:#"user_id"];
if(userId!=nil)
{
NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
collisionId = [NSString stringWithFormat:#"%#",[dict valueForKey:#"collisionId"]];
NSLog(#"%#",collisionId);
// removes saved datas from database
BOOL result = [database removeCollisionDetails:collisionId:#"accident_report"];
if(result)
{
[[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(#"val_sucess_vehicle", nil)];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(#"val_error", nil)];
}
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Warring" message:#"Are You Sure?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes ", nil];
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
}else if (buttonIndex == 1){
NSIndexPath *indexPath=[_UserTableView indexPathForSelectedRow];
[showUData removeObjectAtIndex:indexPath.row]; //showUData NSMutableArray
[_UserTableView reloadData];
[storeData setObject:showUData forKey:#"SendData"]; //storeData NSUserDefault
[storeData synchronize];
}
}
Use UIAlertView and only delete if the user confirms. Update your code to :
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hello World!"
message:#"Are you sure ?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
Implement the UIAlertViewDelegate and move the original delete code to the delegate method where you detect which button has been tapped.

iPad search display controller displaying results starting in second cell

I added a Searchbar and Search Display Controller to my iPad app that use localSearch. I believe I implemented the delegates correctly. The search works fine and displays the results but the issues is that these results start in the second cell. The first cell in the popup display being empty. I double checked to see if the map items count and contents were correct and they were.
The code:
- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
if (![self connected]) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Cannot Connect",nil)
message:NSLocalizedString(#"Please make sure you are connected to the internet and try again.",nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK",nil) otherButtonTitles:nil] show];
}
else if ([mainToolbar isHidden])
{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Search Unavailable",nil)
message:NSLocalizedString(#"Please make sure you aren't drawing an AOI or using a tool and then try again.",nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK",nil) otherButtonTitles:nil] show];
}
else {
// Cancel any previous searches.
[localSearch cancel];
// Perform a new search.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = self.searchBar.text;
request.region = self.mapView.region;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler: ^(MKLocalSearchResponse *response, NSError *error){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if ([response.mapItems count] == 0)
{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"No Results",nil)
message:nil
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK",nil) otherButtonTitles:nil] show];
return;
}
if (error != nil)
{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Map Error",nil)
message:NSLocalizedString(#"Sorry.", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK",nil) otherButtonTitles:nil] show];
return;
}
results = response;
[self.searchDisplayController.searchResultsTableView reloadData];
}
];
}
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [results.mapItems count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *IDENTIFIER = #"SearchResultsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
}
MKMapItem *item = results.mapItems[indexPath.row];
cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.placemark.addressDictionary[#"Street"];
return cell;
}
Here is a screenshot of the issue.
The first row contains the table header.

panning cells with custom action in ios

Hi i am trying to make a tableview with mailbox style panning http://www.mailboxapp.com/ for that i am using this library https://github.com/gloubibou/HHPanningTableViewCell and it is working fine, i swipe the cell and it moves just fine, the problem is that i want to trigger a custom action when i swipe the cell and i have only been able to do it when it is open and then i touch it.
This is the code where the action is happening
#import "TableViewController.h"
#import "HHPanningTableViewCell.h"
#interface TableViewController ()
#property (nonatomic, retain) NSArray *rowTitles;
#end
#implementation TableViewController
#pragma mark -
#pragma mark Initialization
- (id)init
{
self = [super initWithNibName:#"TableViewController" bundle:nil];
if (self != nil) {
self.rowTitles = [NSArray arrayWithObjects:#"Pan direction: None", #"Pan direction: Right", #"Pan direction: Left", #"Pan direction: Both", #"Custom trigger", nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark -
#pragma mark Accessors
#synthesize rowTitles = _rowTitles;
#pragma mark -
#pragma mark Rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.rowTitles count] * 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSInteger directionMask = indexPath.row % 5;
if (cell == nil) {
cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];
// dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
// Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dark_dotted"]];
cell.drawerView = drawerView;
}
if (directionMask < 3) {
cell.directionMask = directionMask;
}
else {
cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;
if (directionMask == 4) {
cell.delegate = self;
}
}
cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];
return cell;
}
- (void)gestureRecognizerDidPan:(UIPanGestureRecognizer*)gestureRecognizer{
}
#pragma mark -
#pragma mark Table view delegate
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger directionMask = indexPath.row;
NSString *celda = [NSString stringWithFormat:#"%d", directionMask];
[cell isKindOfClass:[HHPanningTableViewCell class]];
HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;
if (directionMask == 1) {
if (HHPanningTableViewCellDirectionRight) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
if ([panningTableViewCell isDrawerRevealed]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"2"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark -
#pragma mark HHPanningTableViewCellDelegate
- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"You triggered a custom action"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
#end
I know i could use a Gesture recognizer to trigger the action but i think the library is already doing that.
in this part i trigger an action knowing exatly the cell, where was it paned to and if the back of the cell is revealed or not, but always by clicking it since it is a select function.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger directionMask = indexPath.row;
NSString *celda = [NSString stringWithFormat:#"%d", directionMask];
[cell isKindOfClass:[HHPanningTableViewCell class]];
HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;
if (directionMask == 1) {
if (HHPanningTableViewCellDirectionRight) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
if ([panningTableViewCell isDrawerRevealed]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"2"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
return indexPath;
}
and i believe this other part is where the custom action should be trigered but the program never enters this function
- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"You triggered a custom action"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
I hope i made myself clear and thank you in advance.
Change the delegate method to
- (void)panningTableViewCell:(HHPanningTableViewCell *)cell didTriggerWithDirection:(HHPanningTableViewCellDirection)direction;
For the delegate method to trigger you need to set your controller as delegate for the cell. Currently in your cellForRowAtIndexPath the controller is assigned as delegate only when directionMask is 4. So you either set directionMask to be 4 in your current code (which is returning a value based on the cell position instead) or you set the controller as delegate in every case, as I've done below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSInteger directionMask = indexPath.row % 5;
if (cell == nil) {
cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];
// dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
// Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dark_dotted"]];
cell.drawerView = drawerView;
}
if (directionMask < 3) {
cell.directionMask = directionMask;
}
else {
cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;
// previous code
//if (directionMask == 4) {
// cell.delegate = self;
//}
}
cell.delegate = self;
cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];
return cell;
}

Custom added cell wont save in table view

I have a alert view with a textfield so whenever someone types in it and pressed the save button it puts in the table view. when you click on the cell after being saved it takes you to a different view. Now when you go back to the home page, the cells disappear. I have tried multiple ways of figuring it out, yet still haven't been able to. Do I need to add a plist so every time I add a cell it gets saved to the plist and if so where would i start?
This code is in my table view controller
- (IBAction)add:(id)sender {
NSLog(#"%#",tableData);
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"My Favs" message:#"Hello" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.enablesReturnKeyAutomatically = YES;
alertTextField.placeholder = #"example";
[alert show];
return;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%#",tableData);
//Only do the following action if the user hits the ok button
if (buttonIndex == 1){
NSString *tapTextField = [alertView textFieldAtIndex:0].text;
if (!tableData)
{
tableData = [[NSMutableArray alloc]init];
}
[tableData insertObject:tapTextField atIndex:0];
[myTableView reloadData];
}
}
I think tableData is getting deallocated when you are coming back to the home page. If the tableData is not a huge array you can store it in NSUserDefaults. This way when ever you come back to the table you can always retreat the data. Check out this code. It will store the tableData in the NSUserDefaults every time you add anything to the array. Let me know if this works for you.
#import "ViewController.h"
#interface ViewController () <UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (nonatomic,strong) NSMutableArray *tableData;
#end
#implementation ViewController
-(NSMutableArray *)tableData
{
if(!_tableData){
NSMutableArray *data = [[NSUserDefaults standardUserDefaults]objectForKey:#"data"];
if(!data){
_tableData = [[NSMutableArray alloc]init];
}else{
_tableData = [data mutableCopy];
}
}
return _tableData;
}
- (IBAction)add:(UIButton *)sender {
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"My Favs" message:#"Hello" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.enablesReturnKeyAutomatically = YES;
alertTextField.placeholder = #"example";
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1){
NSString *tapTextField = [alertView textFieldAtIndex:0].text;
[self.tableData insertObject:tapTextField atIndex:0];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.tableData forKey:#"data"];
[defaults synchronize];
[self.myTableView reloadData];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return[self.tableData count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"dataCell" forIndexPath:indexPath];
if(self.tableData.count > 0){
cell.textLabel.text = self.tableData[indexPath.row];
}
return cell;
}

Resources