I have two UITableview's and if i select a one row in each table, the textLabel.text of that particular row should display in a single UIAlertview.
How can i combine the textLabel.textof two tables and display in one UIAlertView
Can anyone let me know how can i do this
EX: one Table view which displays A, B, C, D and one more tableView Which Shows 1,2,3,4.
This two table are from different class's. now suppose if i press a row in table 1 i will get textLabel.text as 'A' and if i press table 2 i will get textLabel.text as '1' now on the view if a select A in table1 and 1 in table 2 i should get a AlertView showing message as 'A1'
Code for reference:
viewController.h
#import <UIKit/UIKit.h>
#import "FirstTVContoller.h"
#import "SecondTVController.h"
#interface TwoTableViewsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
FirstTVContoller *firstController;
SecondTVController *secondController;
IBOutlet UITableView *firstTable;
IBOutlet UITableView *secondTable;
NSString *stringTable1;
NSString *stringTable2;
NSArray * myArray1;
NSArray * myArray2;
}
#property (nonatomic, retain) NSString *stringTable1;
#property (nonatomic, retain) NSString *stringTable2;
#property (nonatomic, retain) NSArray * myArray1;
#property (nonatomic, retain) NSArray * myArray2;
#end
.m:
#import "TwoTableViewsViewController.h"
#implementation TwoTableViewsViewController
#synthesize stringTable1 = stringTable1;
#synthesize stringTable2 = stringTable2;
#synthesize myArray1,myArray2;
- (void)viewDidLoad {
[super viewDidLoad];
if (firstController == nil) {
firstController = [[FirstTVContoller alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTVController alloc] init];
}
[firstTable setDataSource:firstController];
[secondTable setDataSource:secondController];
[firstTable setDelegate:firstController];
[secondTable setDelegate:secondController];
firstController.view = firstController.tableView;
secondController.view = secondController.tableView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == firstTable) {
self.stringTable1 = [myArray1 objectAtIndex: indexPath.row];
//call uiAlert, and place the stringTable1 on your message
if (tableView == secondTable) {
self.stringTable2 = [myArray2 objectAtIndex: indexPath.row];
//call uiAlert, and place the stringTable2 on your message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"hi" message:[NSString stringWithFormat:#"%# %#", self.stringTable1, self.stringTable2] delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[firstController release];
[secondController release];
[firstTable release];
[secondTable release];
[stringTable1 release];
[stringTable2 release];
[super dealloc];
}
#end
table1:
#import <Foundation/Foundation.h>
#interface FirstTVContoller : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *items;
}
#end
#import "FirstTVContoller.h"
#import "SecondTVController.h"
#implementation FirstTVContoller
-(void) loadView
{
if (items == nil) {
items = [[NSMutableArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"6",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",nil] retain];
}
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"MyIdentifier"];
}
cell.textLabel.text = [NSString stringWithFormat:#"1.%#" ,[items objectAtIndex:indexPath.row]];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringVariable = cell.textLabel.text;
NSLog(#"%#",stringVariable);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Delete the object from the table.
[items removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(void) dealloc
{
[items release];
[super dealloc];
}
#end
Table2:
#import <Foundation/Foundation.h>
#interface SecondTVController : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
int numberOfCells;
}
#end
#import "SecondTVController.h"
#implementation SecondTVController
-(void) viewDidLoad
{
numberOfCells = 20;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return numberOfCells;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:#"MyIdentifier"];
}
cell.textLabel.text = [NSString stringWithFormat:#"2.%d", indexPath.row];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringVariable = cell.textLabel.text;
NSLog(#"%#",stringVariable);
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Delete the object from the table.
numberOfCells -=1;
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
}
#end
Kindly Suggest
Is not clear to me when you want your alertView shown,
but this will help you
you need to have a property for placing the text of your labels
so on your .h
# interface MyViewController :UIViewController {
NSString *_stringTable1;
NSString *_stringTable2;
}
#property (nonatomic, retain) NSString *stringTable1;
#property (nonatomic, retain) NSString *stringTable2;
on your .m
#synthesize stringTable1 = _stringTable1;
#synthesize stringTable2 = _stringTable2;
- (void) dealloc{
[_stringTable1 release];
[_stringTable2 release];
[super dealloc];
}
so on your table delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == table1) {
self.stringTable1 = [myArray1 objectAtIndex: indexPath.row];
//call uiAlert, and place the stringTable1 on your message
}
if (tableView == table2) {
self.stringTable2 = [myArray2 objectAtIndex: indexPath.row];
//call uiAlert, and place the stringTable2 on your message
}
}
when you call your uiAlertView, for the message you can just show them togheter like
[NSString stringWithFormat:#"%# %#", self.stringTable1, self.stringTable2];
//Combine two string in one string on the tap of second table cell tap event...
//Use following code in didSelectRowAtIndexPath method..and enjoy...
NSString *tempstring = [[NSString alloc]init];
tempstring =yourlable.text;// your cell label...
if (textField.tag == 1) {
NSUserDefaults *stringsaver = [NSUserDefaults standardUserDefaults];
if([stringsaver objectForKey:#"stringsaver"]== nil)
{
[stringsaver setObject:tempstring forKey:#"stringsaver"];
}
else
{
NSString *combinedstring = [stringsaver objectForKey:#"stringsaver"];
//NSLog(#"==%#",combinedstring);
UIAlertView *alertdata = [[UIAlertView alloc]
initWithTitle:#"Your Title"
message:combinedstring
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertdata show];
[alertdata release];
[stringsaver removeObjectForKey:#"stringsaver"];
}
Try this-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell1 = [tableView cellForRowAtIndexPath:indexPath];
// get here the instance of otherTableView.
UITableViewCell * cell2 = [otherTableView cellForRowAtIndexPath:indexPath];
UILabel *Title1 = (UILabel *)[cell1 viewWithTag:1]; //for it you need to make the label with Tag 1
UILabel *Title2 = (UILabel *)[cell2 viewWithTag:1]; //for it you need to make the label with Tag 1
NSString * str = [NSString stringWithFormat:#"%# %#",[Title1 text],[Title2 text]];
//fire the alert
UIAlertView *alertdata = [[UIAlertView alloc]
initWithTitle:#"Your Title"
message:str
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertdata show];
}
Related
I'm a beginner and I've read everything on StackOverflow about my problem - my app with json data doesn't show anything on TableViewController. I'm probably missing something obvious, but the help is appreciated very very much. (I'm using the latest Xcode 5 DP, if it's important).
TableVC.h
#interface TableVC : UITableViewController <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) NSDictionary *kinos;
#property (retain, nonatomic) UITableView *tableView;
-(void)fetchKinos;
#end
And the TableVC.m file is
#interface TableVC ()
#end
#implementation TableVC
- (void)viewDidLoad
{
[self fetchKinos];
[self.tableView reloadData];
[super viewDidLoad];
}
-(void)fetchKinos {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.adworldmagazine.com/json.json"]];
NSError *error;
_kinos = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _kinos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"KinoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//[self configureCell:cell atIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *entities = [_kinos objectForKey:#"entities"];
NSDictionary *kino = [entities objectAtIndex:indexPath.row];
NSDictionary *title = [kino objectForKey:#"title"];
NSString *original = [title objectForKey:#"original"];
NSString *ru = [title objectForKey:#"ru"];
cell.textLabel.text = original;
cell.detailTextLabel.text = ru;
return cell;
}
#end
Your JSON response dictionary contains an array of entities whose count needs to be returned from the table view method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.kinos objectForKey:#"entities"] count];
}
Also, a suggestion, when you declare strong properties, try to access them as self.propertyName instead of accessing the ivar like _propertyName.
Hope that helps!
I accessed your url- http://www.adworldmagazine.com/json.json on my browser.
The response returns a JSON with root: dictionary.
So,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _kinos.count;
}
wont work.
Use this instead:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_kinos objectForKey:#"entities"].count;
}
When I make an object class conform to NSCoding, the tableView does not show new entries. Suspected it had to do with the object initialization but cannot solve it. Might be very basic but cannot find the error. Here is the code I simplified to post:
Custom Object:
// DataObject.h
#import <Foundation/Foundation.h>
#interface DataObject : NSObject {
NSString *name;
}
#property (nonatomic, copy) NSString *name;
#end
// DataObject.m
#import "DataObject.h"
#implementation DataObject
#synthesize name;
- (void)encodeWithCoder:(NSCoder*)encoder {
[encoder encodeObject:name forKey:#"name"];
}
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) return nil;
name = [[decoder decodeObjectForKey:#"name"] retain];
return self;
}
These is the TableView root controller - Have omitted a few methods:
// RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
NSMutableArray *list;
}
#property (nonatomic, retain) NSMutableArray *list;
- (void)add:(id)sender;
- (NSString *)dataFilePath;
#end
// RootViewController.m
#import "RootViewController.h"
#import "DataObject.h"
#implementation RootViewController
#synthesize list;
- (void)add:(id)sender
{
DataObject *newEntry = [[DataObject alloc] init];
newEntry.name = #"Willy";
[self.list addObject:newEntry];
[self.tableView reloadData];
// Scroll table view to last row
if ([list count] > 1) {
NSUInteger index = list.count - 1;
NSIndexPath *lastRow = [NSIndexPath indexPathForRow:index inSection: 0];
[self.tableView scrollToRowAtIndexPath: lastRow atScrollPosition: UITableViewScrollPositionTop animated: YES];
}
[newEntry release];
}
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:#"datafile"];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = #"Names";
self.list = [[NSMutableArray alloc] init];
NSMutableArray *tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
self.list = tempArray;
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:app];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(add:)];
self.navigationItem.leftBarButtonItem = addButton;
[addButton release];
[self.tableView reloadData];
}
- (void)applicationWillResignActive:(NSNotification *)notification;
{
[NSKeyedArchiver archiveRootObject:self.list toFile:[self dataFilePath]];
}
- (void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
[super viewWillAppear:animated];
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [list 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] autorelease];
}
// Configure the cell.
NSUInteger row = [indexPath row];
DataObject *oneName = [self.list objectAtIndex:row];
cell.textLabel.text = oneName.name;
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
NSUInteger row = [indexPath row];
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#end
Oh, man I am so embarrassed...It had nothing to do with NSCoding directly but when I made the data class NSCoding compliant I added in viewDidLoad:
NSMutableArray *tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
self.list = tempArray;
The first line is fine, but the second assigned a new pointer address to self.list and the tempArray was later autoreleased...
As this line was not executed in the "not NSCoding compliant" version it appeared to work then and not when encodeWithCoder and initWithCoder were implemented.
Because of this self.list was nil instead of being initialized and ready when in the add: method I added a new instance. It was created fine, but never added to the array and therefore never showed in the tableView.
This was easily fixed using instead in viewDidLoad:
NSMutableArray *tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
[self.list addObjectsFromArray:tempArray];
Worst part is that I was pulling my hair because of this and was convinced it was because of NSCoding and the initialization of objects...
Please can any one correct my silly mistake as i am unable to show my parsed XML to the Table view.
I want my tag to be displayed on table view.
here is my code (ignore my syntax ... as i am unable to paste it correctly here)....
XML TO BE PARSED
<Table><category><Name>Books</Name><cid>2</cid><Logo>http://litofinter.es.milfoil.arvixe.com/Thumbnail/5.png</Logo><Product><pid>55</pid><Title>Un producto para cada necesidad</Title><Thumbnail>http://litofinter.es.milfoil.arvixe.com/Thumbnail/Book3.png</Thumbnail><pdf>http://litofinter.es.milfoil.arvixe.com/PDF/Book6.pdf</pdf></Product><Product><pid>58</pid><Title>Quitamanchas pistola</Title><Thumbnail>http://litofinter.es.milfoil.arvixe.com/Thumbnail/Book9.png</Thumbnail><pdf>http://litofinter.es.milfoil.arvixe.com/PDF/Book7.pdf</pdf></Product></category></Table>
CODE
#TABLEVIEWCONTROLLER.H:
#class Book;
#interface TableViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> {
IBOutlet UIButton *btnBack;
IBOutlet UITableView *tableView;
Book *bookOne;
NSMutableArray *array;
}
-(IBAction)onTapBack;
#property (nonatomic, retain)IBOutlet UITableView *tableView;
#end
#TABLEVIEWCONTROLLER.M
import "TableViewController.h"
import "XMLTableAppDelegate.h"
import "Book.h"
#implementation TableViewController
#synthesize tableView;
-(IBAction)onTapBack
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Message from mAc" message:#"Trail" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok",nil];
[alert show];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
Book *book = [[Book alloc]init];
book = [array objectAtIndex:indexPath.row];
cell.textLabel.text = book.arrayString;
[book release];
[cell autorelease];
return cell;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
tableView.delegate = self;
tableView.dataSource = self;
//bookOne = [[Book alloc]init];
array = [[NSMutableArray alloc]init];
[array addObject:#"mAc"];
[array addObject:#"Mayank"];
[array addObject:#"Manu"];
}
-(void)viewWillAppear:(BOOL)animated{
[tableView reloadData];
}
- (void)dealloc {
[super dealloc];
[tableView release];
[btnBack release];
}
#BOOK.h
#interface Book : NSObject {
NSMutableString *arrayString;
}
#property(nonatomic,retain) NSMutableString *arrayString;
#end
#BOOK.M
import "Book.h"
#implementation Book
#synthesize arrayString;
-(void)dealloc
{
[super dealloc];
[arrayString release];
}
#end
You shouldn't alloc+init memory for Book and don't release it : [book release];
Your array contains objects of class NSString : [array addObject:#"mAc"];
Try to set value using next line: cell.textLabel.text = [array objectAtIndex:indexPath.row];
You do like this,
book.arrayString= [array objectAtIndex:indexPath.row];
This also shows the data you added by your own (ie) that mAc,Mayank and something.
Otherwise You diectly add the value like
Cell.textLabel.text=[array objectAtIndex:indexPath.row];
I have got an assignment here where I am supposed to
create an application, which copies the all the pasteboard items into
a table, that drills down into a detail view of said pasteboard item
.
Below, I give you my code of the RootViewController.m file. The program does not work and gives me an error of SIGABRT in this line cell.textLabel.text = cellValue;.
Could you tell me please, what could be possibly wrong here and thank you in advance.
#import "RootViewController.h"
#implementation RootViewController
#synthesize detailsViewController;
NSArray* pasteBoardItems;
- (void)viewDidLoad
{
// Get a reference to the system pasteboard
UIPasteboard* pasteBoard = [UIPasteboard generalPasteboard];
NSLog(#"%#", pasteBoard.items);
pasteBoardItems = [pasteBoard.items valueForKey:#"public.utf8-plain-text"];
pasteBoardItems = [pasteBoard.items valueForKey:#"public.item (kUTTypeItem)"];
self.navigationItem.title = #"Pasteboard";
[super viewDidLoad];
}
- (void)dealloc
{
[pasteBoardItems release];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [pasteBoardItems count];
}
// Customize the appearance of table view cells.
- (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];
}
NSString *cellValue = [pasteBoardItems objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// Configure the cell.
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *rowValue = [pasteBoardItems objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:#"You have selected \"%#\"",rowValue];
if(self.detailsViewController == nil){
DetailsViewController *d = [[DetailsViewController alloc] initWithNibName:#"DetailsViewController" bundle:[NSBundle mainBundle]];
self.detailsViewController = d;
[d release];
}
[self.detailsViewController initWithTextSelected:message];
[self.navigationController pushViewController:self.detailsViewController animated:YES];
}
#end
SIGABRT in most cases means that you want to access released object.
In your case, I suggest to add line [pasteBoardItems retain]; at the end of - (void)viewDidLoad method.
first .h
#interface PersonnelViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *personnelData;
IBOutlet UITextField *tableCellText;
IBOutlet UITableView *personTableView;
IBOutlet UINavigationItem *navItem;
}
#property (nonatomic, retain) NSMutableArray *personnelData;
-(IBAction)addRowToTableView;
-(IBAction)editTable;
-(NSString *)personDataFilePath;
-(IBAction)endText;
-(IBAction)done;
first .m
#implementation PersonnelViewController
#synthesize personnelData;
-(IBAction)done{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self personDataFilePath]];
if (archivedArray == nil) {
personnelData = [[NSMutableArray alloc] init];
} else {
personnelData = [[NSMutableArray alloc] initWithArray:archivedArray];
}
}
- (IBAction)addRowToTableView {
[personnelData addObject:tableCellText.text];
[self personDataFilePath];
[personTableView reloadData];
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[personTableView setEditing:!personTableView.editing animated:YES];
if (personTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self personDataFilePath];
[personTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [personnelData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"] autorelease];
}
cell.textLabel.text = [personnelData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)personDataFilePath {
NSString *personDataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
personDataFilePath = [[documentDirectory stringByAppendingPathComponent:#"applicationData.plist"] retain];
return personDataFilePath;
}
- (void)saveData1 {
[NSKeyedArchiver archiveRootObject:[personnelData copy] toFile:[self personDataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[personnelData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[personnelData objectAtIndex:fromIndexPath.row] retain];
[personnelData removeObject:item];
[personnelData insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)dealloc {
[super dealloc];
}
#end
Second .h
#interface ApparatusViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *apparatusData;
IBOutlet UITextField *tableCellText;
IBOutlet UITableView *mainTableView;
IBOutlet UINavigationItem *navItem;
}
#property (nonatomic, retain) NSMutableArray *apparatusData;
-(IBAction)addRowToTableView;
-(IBAction)editTable;
-(NSString *)apparatusDataFilePath;
-(IBAction)endText;
-(IBAction)done;
#end
Second .m
#implementation ApparatusViewController
#synthesize apparatusData;
-(IBAction)done{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
NSArray *arichvedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self apparatusDataFilePath]];
if (arichvedArray == nil) {
apparatusData = [[NSMutableArray alloc] init];
} else {
apparatusData = [[NSMutableArray alloc] initWithArray:arichvedArray];
}
}
- (IBAction)addRowToTableView {
[apparatusData addObject:tableCellText.text];
[self apparatusDataFilePath];
[mainTableView reloadData];
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[mainTableView setEditing:!mainTableView.editing animated:YES];
if (mainTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self apparatusDataFilePath];
[mainTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [apparatusData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"] autorelease];
}
cell.textLabel.text = [apparatusData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)apparatusDataFilePath {
NSString *apparatusDataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
apparatusDataFilePath = [[documentDirectory stringByAppendingPathComponent:#"applicationData.plist"] retain];
return apparatusDataFilePath;
}
- (void)saveData {
[NSKeyedArchiver archiveRootObject:[apparatusData copy] toFile:[self apparatusDataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[apparatusData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[apparatusData objectAtIndex:fromIndexPath.row] retain];
[apparatusData removeObject:item];
[apparatusData insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)dealloc {
[super dealloc];
}
#end
The data seems to be loading the same data into each table. How do I fix this.
You are loading "applicationData.plist" in both views!