UITableView not displaying data from array - iphone

Code:
In .h:
NSMutableArray *contentArray;
I'm declaring my array.
In .m
- (void)viewDidLoad
{
[super viewDidLoad];
contentArray = [[NSMutableArray alloc] initWithObjects:#"view", #"browse", #"create", nil];
// 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;
}
I'm setting it up in my view did load.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [contentArray count];
}
I'm setting the number of rows to the array 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...
[[cell textLabel] setText:[contentArray objectAtIndex:indexPath.row]];
return cell;
}
Nothing. But if I do "[[cell textLabel] setText:#"Hello World."];" instead, it works fine.

Are you at least getting 3 empty rows in table ? If yes then just change your code
NSString *tempString = [your_array objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
If you are not even getting empty strings then make property of your array in .h file. Synthesize it in .m (also release it in delloc function) and finally in viewDidLoad right following
NSMutableArray tempContentArray = [[NSMutableArray alloc] arrayWithObjects:#"view", #"browse", #"create", nil];
self.contentArray=tempArray;
and then write following code to get cell title
NSString *tempString = [self.contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;

Try to initialize your Array in your init method or call reloadDataon your UITableView after you set your Array.

Your code in .m file is fine. Write property .h file synthesize and dealloc your NSMutable array .m file it may be help you.

simply use following line of code
//initlize nsarray with objects.
-(void)viewDidLoad
{
NSArray *practice_ArrayForDisplayingEx=[NSArray alloc]initWithObjects:#"bhupi",#"bhupi",#"bhupi",#"bhupi",nil];
}
//then use in tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
// UIButton *practice_takeTest_button;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]autorelease];
}
cell.textLabel.text =[practice_ArrayForDisplayingEx objectAtIndex:indexPath.row];
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:15];
return cell;
}
make sure you included UITableViewDelegate & UITableViewDataSourse prtocol in .h file.
hope it will help you..

Make sure that your .h implements the tableview delegate and datasource... should look like this
#interface myClass : UITableViewController <UITableViewDelegate, UITableViewDataSource>
then make this code in your ViewDidLoad method
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];

Related

How Can We add a row every time on the top of the tableview first row on a button click in view?

Can you please help me to do this one?
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [popOveritem objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont systemFontOfSize:10.05f]];
cell.imageView.image = [UIImage imageNamed:[popimage objectAtIndex:indexPath.row]];
NSLog(#"cellCOntent%#",cell.textLabel.text);
UIView *cellbgColorView = [[[UIView alloc] init]autorelease];
[cellbgColorView setBackgroundColor:[UIColor colorWithRed:40/255.0 green:128/255.0 blue:184/255.0 alpha:1.0]];
cell.selectedBackgroundView = cellbgColorView;
tableVIEW.scrollEnabled = NO;
return cell;
first time the tableview will show one record and when i click to the button appeared in the view will add another row at the top of the table view?
take a look at the below code hope it gives you what you need
viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
IBOutlet UITableView *tblTest;
NSMutableArray *arrData;
int increment;
}
-(IBAction)onClickAdd:(id)sender;
#end
viewcontroller.m
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrData = [[NSMutableArray alloc] initWithObjects:#"Data 1", nil];
increment = 1;
}
-(IBAction)onClickAdd:(id)sender
{
increment++;
NSString *strInsertData = [NSString stringWithFormat:#"Data %d",increment];
[arrData insertObject:strInsertData atIndex:0];
[tblTest reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [arrData 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:nil];
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

NSMutableArray data not loading in tableView in iPhone?

I did XML parsing for the content I obtained from WCF Service. I loaded the data in NSMutableArray using NSXMLParser.Now I want that data to be loaded in tableview in a UIView. But I can see the empty tableview being loaded and data doesn't. Couldn't understand where I am going wrong.
My NSMutableArray is like this:
(
{
UserName="Roy";
Password="126";
},
{
UserName="Joy";
Password="123";
},
{
UserName="Rony";
Password="5983";
}
-------------
-------------
)
And the code for UItableView is :
#interface RootViewController : UIViewController<NSXMLParserDelegate,UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *arr;
UITableView *tableView;
}
- (void)viewDidLoad
{
UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arr 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];
}
NSString *tempstring=[arr objectAtIndex:indexPath.row];
cell.textLabel.text= tempstring;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
I am getting empty tableview with some empty rows. How can i fix this?
hey you just add this code in your cellForRowAtIndexPath delegete:-
EDITED
-(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];
}
SMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row]
UILabel *username = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 280, 30)];
username.textColor =DARK_VIEW;
username.font=[UIFont boldSystemFontOfSize:15];
[username setTextAlignment:UITextAlignmentLeft];
[username setText:[d valueForKey:#"UserName"]];
[cell addSubview:username];
[username release];
UILabel *Password = [[UILabel alloc]initWithFrame:CGRectMake(11, 25, 280, 30)];
Password.textColor =DARK_VIEW;
Password.font=[UIFont fontWithName:#"arial" size:12];
[Password setTextAlignment:UITextAlignmentLeft];
[Password setText:[d valueForKey:#"Password"]];
[cell addSubview:Password];
[Password release];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row]
NSString *forNExtViewString =[d valueForKey:#"EmployeID"];
objSectonView =[[cntrSecodviewcontroller alloc]initWithNibName:#"cntrSecodviewcontroller" bundle:nil];
[self.navigationController pushViewController:objSectonView animated:YES];
}
You forgot to set the tableView delegate and datasource. Your datasource also looks like it contains a collection of NSDictionaries. In your cellForRow you have to correctly extract the values from the dictionary
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
Like Nitin Gohel pointed out
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
cell.textLabel.text = [[arr objectAtIndex:indexPath.row] valueForKey:#"UserName"];
return cell;
}
Just make sure about the following things :
Values are retained in the array
Reload your TableView after the objects are added to array.
i.e [tableView reloadData];
And Just replace your method with this one.
-(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];
}
//Over here just print the value of your array , to cross check whether the values are retained in the Array
NSLog(#"%#",[arr objectAtIndex:indexPath.row]);
NSString *tempstring= [[arr objectAtIndex:indexPath.row] valueForKey:#"UserName"];
cell.textLabel.text= tempstring;
return cell;
}
This would definitely bring the solution of your problem.
Check this and confirm your delegates, that might be the reason of not getting tableview called.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tablView = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, 300, 460)];
tablView.delegate = self;
tablView.dataSource = self;
[self.view addSubview:tablView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell * cell = [tablView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
UILabel *lbl5 = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 250, 50);];
UILabel *lbl6 = [[UILabel alloc]initWithFrame:CGRectMake(30, 60, 250, 50)];
lbl1.text = [arrayName objectAtIndex:i];
lbl2.text = [arrayAddress objectAtIndex:i];
NSString *tempstring=[arr1 objectAtIndex:indexPath.row];
NSString *tempstring=[arr2 objectAtIndex:indexPath.row];
[cell addSubview:lbl1];
[cell addSubview:lbl2];
// cell.textLabel.text= tempstring;
return cell;
}
You are accessing array at indexPath, and this array contain dictionary, so first get array and take it in dictionary and access it with key value
cell.textLabel.text = [[arr objectAtIndex:indexPath.row] objectForKey:#"UserName"];
Your mutable array has elements of dictionary type. so while updating the text in table cell try to grab dictionary from array based on your indexPath.row and then objectForKey: for whichever key you want to use. see code below
you should try to use
cell.textLabel.text = [[yourMutableArr objectAtIndex:indexPath.row] valueForKey:#"UserName"];
cell.detailTextLabel.text = [[yourMutableArr objectAtIndex:indexPath.row] valueForKey:#"Password"];
and you can use the default separater of table cells.

tableview is crashing on clicking on cell or on scrolling in ios 5

I made a demo empty app and add a navigation controller with the view
UINavigationController *navBar = [[UINavigationController alloc]initWithRootViewController:objFirstViewControllerViewController];
[self.window addSubview:navBar.view];
After it i add a table view on the first view controller like this .
UITableView* demotableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
demotableView.delegate = self;
demotableView.dataSource = self;
[self.view addSubview:demotableView];
and the delegate function of the table view and main cell for row function like this way
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = #"Hello this is sample text";
cell.textLabel.minimumFontSize = 12;
cell.textLabel.adjustsFontSizeToFitWidth = TRUE;
cell.textLabel.font = [UIFont fontWithName:#"Copperplate" size:18];
return cell;
}
But when i scroll on my table or click any cell to go on the next view it just crash and give these two error on clicking and scrolling respectively.
[__NSCFArray tableView:didSelectRowAtIndexPath:]
[__NSCFDictionary tableView:cellForRowAtIndexPath:]
I don't understand what is getting wrong this code it have been working with the prior os properly
Any body can help please ?
Here is the code for did select row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
}
and no of row is this one
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 15;
}
Actually I misused the ARC what are the changes I made to make the app run successfully actually it was crashing due to memory leak I reference the class in delegate with local object but the it got released when it tried to add the data on it and when table's delegate and datasource try to add the things in the current class it was released and it throw message from those message instances, I was stuck because I was thinking it is happening due to me having taken an empty kind of application but after adding the lines below in the delegate class I got the problem solved.
What I did in the delegate class in the .h file:
FirstViewControllerViewController *objFirstViewControllerViewController;
#property (strong, nonatomic) FirstViewControllerViewController *objFirstViewControllerViewController;
Then my table started behaving properly and all things that I was having problem in.
Replace your code by this and try:
- (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];
}
cell.textLabel.text = #"Hello this is sample text";
cell.textLabel.minimumFontSize = 12;
cell.textLabel.adjustsFontSizeToFitWidth = TRUE;
cell.textLabel.font = [UIFont fontWithName:#"Copperplate" size:18];
return cell;
}
Hope this helps.
Please try this....
add your table view like this..
productList = [[UITableView alloc] initWithFrame:CGRectMake(0,102, 320, 267) style:UITableViewStylePlain];
productList.delegate = self;
productList.dataSource = self;
productList.backgroundColor = [UIColor clearColor];
[self.view addSubview:productList];
and add these methods....
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (your row 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
}
// Configure the cell.
cell.textLabel.text = #"Title";
cell.detailTextLabel.text = formattedString;
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
}
Since you have set demotableView.delegate = self;
you have to implement the tabelView: didSelectRowAtIndexPath: function which solves the crash on selecting(on Click) a Cell.
To resolve the crash of scrolling, you hav to implement
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
method.
P.S: In the cellForRowAtIndexPath:, all the lines except cell.textLabel.text should be inside
if(cell == nil){
}
and please follow proper maemory management rules

Custom UITableViewCells with a programmatically created table

When Ever I try to load data from an array into my table using custom cells it only shows the last cell like its overwriting all the cells until I Get to the end of the list. The table was created programmatically and when the cells don't load the blank table scrolls. If however I load the cells then only the last cell shows up and it won't scroll.
I figure I made a typo or something but I can't really see anything that would be of much help. The parser works and the array is fine its just that the cells are acting strange. I wanted to make it so that I had a fixed bar over the table as well but first I needed to get the custom cells to show up in the table and scroll properly
schedule.h
#interface Schedule : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *newsTable;
UIActivityIndicatorView *activityIndicator;
CGSize cellSize;
NSXMLParser *fileParser;
NSMutableArray * events;
NSMutableArray * announcements;
NSMutableDictionary * item;
NSString *currentElement;
NSMutableString * currentTitle, * currentDate, * currentSummary, * currentId, * curr entTime, *currentContent;
UITableViewCell *appCell;
}
#property(nonatomic, retain)IBOutlet UITableView *newsTable;
#property(nonatomic, retain)IBOutlet UITableViewCell *appCell;
-(void)parseXMLFileAtURL:(NSString *)URL;
#end
schedule.m
- (void)loadView{
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
tableView.tag = 4;
[tableView reloadData];
self.view = tableView;
[tableView release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [events count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = appCell;
self.appCell = nil;
}
// Configure the cell.
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
int eventIndex = [indexPath indexAtPosition: [indexPath length] - 1];
label.text = [[events objectAtIndex: eventIndex] objectForKey: #"event"];
return cell;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(#"webpage parsed");
NSLog(#"events array has %d items", [events count]);
newsTable = (UITableView*)[self.view viewWithTag:4];
[newsTable reloadData];
}
I added more code the problem may be somewhere else.
I think your problem is that the cell is being released. When you call self.appCell = nil;, the object will be released and the variable set to nil. Try changing cell = appCell; to cell = [[appCell retain] autorelease]; so that the cell is retained long enough for the table view to retain it again.
Also, you could change int eventIndex = [indexPath indexAtPosition: [indexPath length] - 1]; to int eventIndex = indexPath.row;. The row and section properties were added to NSIndexPath specifically for use in UITableView, and it is guaranteed that the index path passed to cellForRowAtIndexPath: will contain exactly two indices.
try with following code;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"CellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
if (cell == nil)
{
NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in objectList) {
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell*)currentObject;
break;
}
}
cell.label.text = [[events objectAtIndex: indexPath.row] objectForKey: #"event"];
}
return cell;
}
when you load the xib file with NSBundle, it will return an array of Bundle. Then you have to check it and then use it.
If you tried like;
NSString *memberName; // member variable
NSString *name = memberName;
memberName = nil;
// the member variable "memberName, giving the reference to name;
//So if you set memberName as nil, name also become a nil value.
// You have to use like;
name = [memberName retain];
memberName = nil;
Try to learn more about iPhone development documents.
Click here for Documentation.
Click here for sample code.
I think it will help you.

initWithCoder and initWithNibName

I am trying to encode some data state in a UITableViewController. In the first time, I init the object with Nibname without any problem. However, when I initWithCoder, the UITableViewController still loads but when I clicked on a cell, the application crash and the debugger tells me about EXEC_BAD_ACCESS, something wrong with my memory, but I do not know
Here is my code:
- (id) init {
if(self = [self initWithNibName:#"DateTableViewController" bundle:nil]) {
self.dataArray = [[NSMutableArray alloc] initWithObjects:#"1", #"2", #"3", nil];
}
return self;
}
// 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];
}
// Set up the cell...
int index = indexPath.row;
cell.textLabel.text = [self.dataArray objectAtIndex:index];;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Test Archiver";
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:self.dataArray];
}
- (id)initWithCoder:(NSCoder *)coder {
return [self init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int index = indexPath.row;
[self.dataArray addObject:[NSString stringWithFormat:#"Hello %d", index]];
[self.tableView reloadData];
}
I see two problems here:
1) You are serializing your dataArray using encodeObject: but omit to deserialize it in initWithCoder:
2) You should rather use NSKeyedArchiver's method encodeObject:forKey:. Then you are free to use the serialized information in the decoding or not.
Other than that I can only say to answer your question it would be helpful to include information about where the crash happens.
There is only one memory problem in this code: In init you are leaking the NSMutableArray. But that’s kind of the opposite problem you are looking for. The bug you described is not in the posted code.