Re: Adding Rows to a Table View
Re: Adding Rows to a Table View
- Subject: Re: Adding Rows to a Table View
- From: Dave <email@hidden>
- Date: Thu, 1 Apr 2010 12:38:24 +0100
Hi All,
I found the first problem, self.mFactTableView was set to nil! I
should have checked this for this obvious error - I could kick
myself! However when I went to setup self.mFactTableView another
problem presented itself.
The way I have my project setup is that I have a View Controller that
contains a button and a TableView field. I created this in IB). When
the App is first launched, the system calls
"numberOfSectionsInTableView" and "tableView:numberOfRowsInSection:"
with the Table View from the NIB file. If I hardwire the number of
rows and the data for these rows, the Table View gets the data ok and
all seems ok. However, I don't have a reference to this Table View
Object in my code.
In order to test this I added the following line to
numberOfSectionsInTableView:
self.mFactTableView = theTableView;
e.g set the "mFactTableView" to the view that was sent to
numberOfSectionsInTableView by the system.
I am assuming that this is a VERY bad thing to do and I only did it
to see if the TableView would respond, which is does however it
crashes shortly afterwards presumably because of the above badness.
So, my question is, what is the correct way to setup "mFactTableView"
from the NIB file? Or is this the wrong way to go about this?
All the Best
Dave
Hi Fritz and Matt,
Thanks for taking the time to reply.
On 1 Apr 2010, at 00:30, Matt Neuburg wrote:
And one other thought - you say you're gathering this data in a
"secondary
thread", so be sure to jump out to the main thread before calling
something
like insertRowsAtIndexPaths:withRowAnimation or reloadData. m.
I confirm that the method that calls
insertRowsAtIndexPaths:withRowAnimation/reloadData is called from the
Main Thread.
On 31 Mar 2010, at 21:17, Fritz Anderson wrote:
Fix this first; I'm surprised it doesn't crash your app almost
immediately:
On 31 Mar 2010, at 1:52 PM, Dave wrote:
...
[myObjectNameString release];
[myFactYearString release];
[myFactMonthString release];
[myFactDayString release];
[myFactSourceDatabaseString release];
[myFactTextString release];
Review the memory-management rules. You don't take ownership of any
of the objects you get from myDictionary, and you must not release
them. If you're using Xcode 3.2 or later, try Build > Build and
Analyze from time to time. If you're not, Snow Leopard is only $30.
The "release" calls were actually if'ed out of the version I was
running. This was the remnants of some debug code I'd added ages ago,
I've removed the offending lines now.
I actually have Snow Leopard, but I'm using Leopard at the moment. I
can easily move over to Snow Leopard, I will do that over the weekend.
For the rest: If it were me, I'd note that
insertRowsAtIndexPaths:withRowAnimation: takes an array, allocate
an NSMutableArray and accumulate the paths into it at each
iteration of the loop, not doing the insertRows..., reloading the
table, or the re-titling of the controller until I was out of the
loop.
After that, I'd break after the end of the loop, and see if the
index-path array contained as many objects as I expected, and that
they were all unique. I think I'd have a better picture then.
— F
I've recoded the method so that it does as you said and build an
array and then just makes one call to
"insertRowsAtIndexPaths:withRowAnimation:". However it still fails to
update the table. None of the expected call backs are called. I've
tried this with both the row and section starting at 0 and at 1.
Could someone tell me what the correct index starting point is? (e.g.
0 or 1).
"numberOfSectionsInTableView" -- Gets called once and always
returns 1.
"tableView: numberOfRowsInSection" -- Gets called once and returns
the number of items in the initially empty array (0).
None of the other TableView call backs gets called.
I've tried it with/without calls to reloadData. I've stepped through
the code and at the end of the loop there is the correct number of
items in the "myIndexPathArray". In the most case the number of items
passed to the "parser: didParseItems" will be 10. This is the current
size of the batch of items to buffer before updating the main Array
and the TableView.
Any ideas on what to try next? I've googled this and found a number
of threads from people with the same or similar problems but have not
found anything on what is causing the problem and how to fix it.
Thanks again for your help.
All the Best
Dave
I've copied the code below
- (void)parser:(ParserBase*)theParser didParseItems:(NSArray*)
theItemDictionaryArray
{
NSEnumerator* myArrayEnumerator;
NSDictionary* myDictionary;
NSString* myObjectNameString;
NSString* myFactYearString;
NSString* myFactMonthString;
NSString* myFactDayString;
NSString* myFactSourceDatabaseString;
NSString* myFactTextString;
static int myObjectCount = 0;
NSMutableArray* myIndexPathArray = nil;
int myItemCount;
NSIndexPath* myIndexPath;
myItemCount = [theItemDictionaryArray count];
[self.mFactDetailArray addObjectsFromArray:theItemDictionaryArray];
myIndexPathArray = [[NSMutableArray alloc] init];
myArrayEnumerator = [theItemDictionaryArray objectEnumerator];
while (myDictionary = [myArrayEnumerator nextObject])
{
myIndexPath = [NSIndexPath indexPathForRow:myObjectCount inSection:0];
[myIndexPathArray addObject:myIndexPath];
myObjectNameString = [myDictionary objectForKey:[ParserXML
parserObjectFieldName]];
myFactYearString = [myDictionary objectForKey:kField_FactYear];
myFactMonthString = [myDictionary objectForKey:kField_FactMonth];
myFactDayString = [myDictionary objectForKey:kField_FactDay];
myFactSourceDatabaseString = [myDictionary
objectForKey:kField_FactSourceDatabase];
myFactTextString = [myDictionary objectForKey:kField_FactText];
NSLog(@"********************** item:%d",myObjectCount);
#if 0
NSLog(myObjectNameString);
NSLog(myFactYearString);
NSLog(myFactMonthString);
NSLog(myFactDayString);
NSLog(myFactSourceDatabaseString);
NSLog(myFactTextString);
#endif
myObjectCount++;
}
[self.mFactTableView beginUpdates];
[self.mFactTableView setEditing:YES animated:YES];
[self.mFactTableView insertRowsAtIndexPaths:myIndexPathArray
withRowAnimation:UITableViewRowAnimationBottom];
//[self.mFactTableView reloadData];
[self.mFactTableView endUpdates];
[myIndexPathArray release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)theTableView
{
return 1;
}
- (NSInteger)tableView:(UITableView*)theTableView
numberOfRowsInSection:(NSInteger)theSection
{
int myCount;
myCount = [self.mFactDetailArray count];
return myCount;
}
- (UITableViewCell *)tableView:(UITableView*)theTableView
cellForRowAtIndexPath:(NSIndexPath *)theIndexPath
{
static NSString* myFactCellIdentifier = @"FactCell";
UITableViewCell* myNewCell = nil;
NSDictionary* myDictionary = nil;
NSString* myObjectNameString = nil;
NSString* myFactYearString = nil;
NSString* myFactMonthString = nil;
NSString* myFactDayString = nil;
NSString* myFactSourceDatabaseString = nil;
NSString* myFactTextString = nil;
//static int myObjectCount = 0;
myNewCell = [theTableView
dequeueReusableCellWithIdentifier:myFactCellIdentifier];
if (myNewCell == nil)
{
myNewCell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:myFactCellIdentifier] autorelease];
}
myDictionary = [self.mFactDetailArray objectAtIndex:theIndexPath.row];
myObjectNameString = [myDictionary objectForKey:[ParserXML
parserObjectFieldName]];
myFactYearString = [myDictionary objectForKey:kField_FactYear];
myFactMonthString = [myDictionary objectForKey:kField_FactMonth];
myFactDayString = [myDictionary objectForKey:kField_FactDay];
myFactSourceDatabaseString = [myDictionary
objectForKey:kField_FactSourceDatabase];
myFactTextString = [myDictionary objectForKey:kField_FactText];
myNewCell.textLabel.text = myFactSourceDatabaseString;
return myNewCell;
}
- (BOOL)tableView:(UITableView *)theTableView canEditRowAtIndexPath:
(NSIndexPath *)theIndexPath
{
return YES;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40looktowindward.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden