Fetching and displaying more records on UITableView - iphone

Hi I have about 2500 records to be displayed. All these data are coming from a MySQL database. The data will be shown 25 at a time in UITableview similar to itunes store. On click of Load More, I need to fetch the next 25 records.
Note:There is no image only texts.
Have any one done anything similar?Give me the sample code.

first in .h define int row;
and now in viewDidLoad: row=25;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([your array count]>row)
return row+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row<row)
{
cell.textLabel.text=#"Your Text";
}
else{
cell.textLabel.text=#"Load More";
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
if (indexPath.row==row)
{
row=row+25;
[tableView reloadData];
}
`
hope this will help you..

Related

What the sequence of UITable view Datasource and delegate when we reload data from table?

Hello I'm beginner in iOS In one of my activity I want to reload table data ...then I have applied this code .....
-(void)buttonpress
{
for(int i=0;i<buttonarr.count;i++)
{
NSString *butt=[NSString stringWithFormat:#"%#",[buttonarr objectAtIndex:i]];
for(int j=0;j<LabelTag.count;j++)
{
NSString *labelbutt=[NSString stringWithFormat:#"%#",[LabelTag objectAtIndex:j]];
if([butt isEqualToString:labelbutt])
{
NSIndexPath *indexPath1 =[NSIndexPath indexPathForRow:j inSection:0];
NSLog(#"indexPath %#",indexPath1);
UITableViewCell *mycel=[self->table_AdviseTestDetails cellForRowAtIndexPath:indexPath1];
NSLog(#"cell %#",mycel);
UILabel *mybutton=(UILabel*)[mycel viewWithTag:100+j];
// mybutton.selected=YES;
NSLog(#"label %#",mybutton.text);
r=r+[mybutton.text floatValue];
NSLog(#"r %f",r);
}
}
}
range=1;
[table_AdviseTestDetails reloadData];
netcharges.text=0;
z1=0;
NSLog(#"label arr %#",LabelArr);
for(int i=0;i<LabelArr.count;i++)
{
NSString *y= [NSString stringWithFormat:#"%#",[[LabelArr objectAtIndex:i] valueForKey:#"value"]];
z1=z1+[y integerValue];
netcharges.text=[NSString stringWithFormat:#"%.2f",z1];
NSLog(#"net %#",netcharges.text);
//balance.text=[NSString stringWithFormat:#"%.2f",z];
}
netcharges.text=[NSString stringWithFormat:#"%.2f",[netcharges.text floatValue]-r];
NSLog(#"net %#",netcharges.text);
point.text=[NSString stringWithFormat:#"%.f",(floor([netcharges.text integerValue]/[[[Point_Result objectAtIndex:0]valueForKey:#"Value"] integerValue]))*[[[Point_Result objectAtIndex:0]valueForKey:#"Points"] integerValue]];
balance.text=netcharges.text;
NSLog(#"net %#",balance.text);
}
This is my code please ignore all code think is there [table_AdviseTestDetails reloadData];
when we reload table then my controller go in table datasource ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and after this controller come below line of this
[table_AdviseTestDetails reloadData];
and after this controller goes in ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I have problem there .....I want when we reload table then first all datasource and delegate of UITableview run and then run other thing.....
So IN my above code ....I want fist reload table data use this
[table_AdviseTestDetails reloadData];
then run all delegate and datasource of table.....
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and controller come after this below of [table_AdviseTestDetails reloadData];
this line .....how can do this ..please solve this problem ......thank in advance
these is the general sequence i am not including all the methods just required ones are here
#pragma mark
#pragma mark - Table view data source
1) -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2) -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3) -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
4) -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
#pragma mark
#pragma mark - Table view delegate
1) -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

how can create more than one UITableView in a UIViewController programmatically?

I want to create 2 UITableView programmatically in a UIViewController. I have 2 NSMutableArray for the datas which are *firstTableDatas and *secondTableDatas. I can manage to create one UITableView programmatically but when it becomes 2 then I am confused what will be the return values for the 2 UITableView for the delegates:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
}
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
and how can i load the datas in 2 individual UITableView s.
Thanks in advance.
use
if(tableView ==Firsttable)
{
...
}
else
{
...
}
in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
...
}
You can check like below:
if(tableView ==firstTableDatas)
{
//Code related to First Table
}
else if(tableView ==secondTableDatas)
{
//Code related to Second Table
}
It will work in UITableViewDelegate methods.
Happy Coding.
give diff tag to both the tableView and on the bases of there tags do the required function
for example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView.tag==1)
{
return 100;
}
else if(tableView.tag==2)
{
return 50;
}
return 40;
}

Why is the rowHeight datasource method not being called

It is my fervent dream to have a UITableView with different row sizes, determined programmatically. I have read the UITableViewDataSource documentation, and I implement these methods:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
which, we can see here:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return (CGFloat) 200.0f;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* test = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil ];
test.textLabel.text = #"TEST";
CGRect ffframe = test.frame;
ffframe.size.height *= 200.0f;
test.frame = ffframe;
return test;
}
These are just trying to make a simple table, with one row, and a height of 200.0. I try to return the height in the delegate method, and set it explicitly. Neither works.
I try trapping the heightForRow method in the debugger, it seems to never be called.
MY table rows are always the size I set in interface builder.
I have the datasource hooked up right, otherwise it would not get the rows and textLabel right.
This is a delegate method so ensure that you have
self.tableview.delegate = self;
in viewDidLoad or else have the tableview delegate property in the xib linked up to the controller.
Careful, this method is a tableview delegate not dataSource method.

Correct operation of Table View and Table Detail View

I need a small help. In my project there is a tab bar. On one of the tab bar items there is a navigation controller, on the view there is a table view.
For TableViewController there are the usual codes:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// here the DetailViewController is activated
}
The construction of DetailViewController is similar to the TableViewController. The data related to the selected row of TableView will be the key data for the SQL query, the result of which is then stored in an array. The only difference is that I use this:
viewWillAppear: includes and SQL query, then loads the result into the array sqlQueryResult.
- (void)viewWillDisappear:(BOOL)animated {
while( [sqlQueryResult count] >0 )
[sqlQueryResult removeObjectAtIndex:0];
}
This works the very first time when the table DetailView is shown. However, if we go back to TableView and select a different row, DetailView appears but the followings will not run again (as they did already): numberOfSectionsInTableView: or, tableView:(UITableView *)tableView numberOfRowsInSection: . I read in the docs that there are methods that are called only once - however I haven't seen such remark for these ones.
How can I "reset" the TableView? Or what methods should I call to set the number of its rows/sections?
What you're looking for I believe is:
[self.tableView reloadData];
You basically need to send a 'reloadData' message to your tableView. It will then call your methods again to refresh its contents.

How to disable the click option or selecting the row in a table in iphone?

I am new to iphone development.I am displaying a xml parsed contents in a grouped tableView.I want disable the click event on it(i should not be able to click it at all) .Since it is grouped table , it contains two tables and i want to disable the first table only and not the second table.How can i achieve it?Please help me out.Thanks.
If you don't want the user to be able to click on a table view, just use this code:
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}
Use Uitable View delegates and data Sources
//#endif
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"Returning num sections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Returning num rows");
return [copyListOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}