Show Loading Message if table takes a long time - iphone

I want to display a "Loading" message if my table is not filled with all the information in 3 seconds. Here is what I want my table to do:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//First time
UITableView *cell = [tableView cellForRowAtIndexPath:indexPath];
//Configure table
//Second Time
}
NSTimeInterval timeDifference = [firstTime timeIntervalSinceDate:secondTime];
if (timeDifference == 3 seconds){
//Display Loading message
}

I think you're approaching the problem wrong in your question. Instead of only showing a loading state if your data loading is taking too long, try always showing the loading state UNTIL the data load operation has completed (hopefully on a background thread which will allow your main UI thread to actually show the loading UI). There are a number of ways to achieve this, the best way for you will depend on your personal preference and existing code.

Related

Display Alert if tableView has no Results

I previously thought I could answer this question with a simple array count (my mistake). I am trying to display an alert to the user id a tableView has no results. I cannot simply place the count in the viewDidAppear method because the web query that populates the JSON and tableView takes a few seconds to populate. I cannot simply place this in:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows]
lastObject]).row){
//loading complete
}
}
Because nothing gets called in this method if there are no results. Does anyone have any recommendations for this - been searching for over 4 hours and have pulled nearly all my hair out.
Please feel free to un-downvote this questions if it has been made more suitable for SO - this questions massive downvotes have cost me my ability to ask questions...
NSInteger count = [array count];
NSlog("array count : %d",[array count];
This will give you array count in your console.
You cannot check the row count in viewDidAppear because an asynchronous NSURLConnection is used to fetch the JSON data that populates the table view.
The correct way is to call reloadData in connectionDidFinishLoading, after you have updated your data source with the response from the URL request. At that point you know if the number of rows is zero or not, and there is no need to wait for the table view update to complete.

iPhone SDK SSCollectionView load item from thread

I have an SSCollectionView which is modeled after NSCollectionView and UITableView. It calls a function to load each item at an indexPath.
I want to asynchronously load images into each one of those paths.
- (SSCollectionViewItem *)collectionView:(SSCollectionView *)aCollectionView itemForIndexPath:(NSIndexPath *)indexPath {
....
[self startLoadingItem:item indexPath:indexPath];
return item;
}
-(void)startLoadingItem:(SSCollectionViewItem *)collectionViewItem indexPath:(NSIndexPath *)indexPath
{
[SEThreadInvocation detachNewThreadSelector:#selector(loadItem:indexPath:) toTarget:self withObjects:collectionViewItem,indexPath, nil];
}
SEThreadInvocation is just a subclass of NSThread so that I can send more than one object to a selector.
-(void)loadItem:(SSCollectionViewItem *)collectionViewItem indexPath:(NSIndexPath *)indexPath {
...
collectionViewItem.imageView.image = imageToDisplay;
}
What is happening is that the code initially works, but every time the collection reloads (often) it puts images all over the collection view randomly.
I believe this is because my SSCollectionViewItem is pointing to a different memory location each time and my item is not thread safe, so it is just stamping my images all over the place when they change in memory.
Also, I thought it might be replacing the images in memory, so I replaced it with numbers and the numbers bounce all over the collection view when reloading as well.
How can I make my objects thread safe in this context?
SSToolkit has a sample Catalog project which has a SSCollectionView demo. It loads images in the way you are needing.

How to implement "Load More Records" in TableView in iPhone SDK?

I have 10,000 rows in database table. I have a view which is suppose to list all these rows in a tableview. But loading all in one shot takes ages to appear in tableview.
How can we use "Load More Records" feature which will fetch 20 records at a time? If user wants to view more entries, they can click "Load More Records" button and it will show next 20 records.
Will have to modify my select statement? What other changes do I have to do to achieve this?
UITableView reuses its cells. This means you can dynamically access your database when cellForRowAtIndexPath: is called- you don't need to load all 10000 elements at once, nor should you. I hope this helps, and that I'm not misunderstanding your question.
This is pretty basic UITableView stuff, but it should get you started. Sorry I don't know much about either Core Data or SQLite.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell-iPad" owner:self options:nil] lastObject];
}
else{
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] lastObject];
}
}
// Do stuff, load database values, etc
return cell;
}
Thanks everyone for your inputs. But I was able to implement this using below link:
http://useyourloaf.com/blog/2010/10/2/dynamically-loading-new-rows-into-a-table.html
Hope this is useful to all.
#meetpd its not an answer just a suggestion.
You can use core data instead of sqlite. it will satisfy your requirement of fetching 20 records. It will reduce your code base to 40% (as said by Bard Larson in the Advance Iphone App Development). It is easy to use and faster than sqlite. You can check out the sample code like this :
hope this will help you a great deal of coding.

Why are multiple touches causing my iPhone app to crash?

In my app, I am getting the row index as the user taps on the row or selected row. But if a row is tapped twice, my app crashes.
What could be causing this behavior, and how can I fix it? Here's the code I'm using:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
abc *xyz = [[abc alloc] init];
dcs = [allHadits objectAtIndex:indexPath.row];
hk = dcs.kokid;
[dcs release];
}
Do you mean tapping on the same row twice makes it crash? If so, it might be because of the [dcs release]. I don't know what dcs is (unless that's supposed to be xyz) but grabbing a pointer to the object in the array and then calling release on it might be releasing the object in the array, making it crash next time the row is hit. Delete the [dcs release] and see if it still crashes. My memory management isn't the greatest though so I could be wrong.
Don't release dcs. Why are your variables named so poorly?

parsing xml for iphone

can i parse my XML in this method
Blockquote
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// here some instance of XMLparser ??
}
Blockquote
like whenevrr uer press table cell then for detail view i want to pull data at that time only and for that specfic only as i have 8k data so i dont want to parse other data
You have to be careful about runtime performance. If the parse runs really fast, like 50 - 100 ms, it is kind of acceptable performance for me. If it runs too long, like > 1 second, it is absolutely sure that you shouldn't put parsing in that method.
If you talk about pulling data from network and parsing it, it is quite risky and slow for me. If you use synchronous, it will block the UI, if you use asynchronous, what will happen when user going to detail view and don't see anything. If it is just like update old data, it is acceptable to use asynchronous
I've used TouchXML and NSURLConnection in the past for doing these kind of things. You'll probably want to use the asynchronous methods so you don't block the GUI thread during the fetches.
yes i am calling in
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch(indexPath.section)
{
case 2:
pragma mark postcode
cell.textLabel.text = aBook.telnumber;
NSLog(#"String = %#",aBook.tel);
NSLog(#"String = %#",aBook.telnumber);
}
tel was defined in sql so that was working correct
now in xml i hav changes that tel to telnumber thats it and commnet that aBook.tel in sql query nothing else
but now its null ... do i need to add xmlparsed elemtns to books in didEndElement in PArsing class??