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

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;
}

Related

Height of cells in different tableViews in iOS

In my view, i have three different tables. For first two tables, i have a fix height for each cell(e.g. say 30.0f for each cell of first table and 45.0f for each cell of second table) which i have adjusted in the storyboard according to the data in each table. For the third table, i want to have a different height for each cell. I tried the method
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
But the issue with this technique is that this method is called for each table. As for the first two tables, i have adjusted the height in storyboard, i don't know what to do with them.And if i do:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == myTable) {
return 150.0;
}
}
As i have nothing to return for the else cases, the whole thing fails.
Any suggestion?
You need to return a height for the other tables.
If you have set there height in the storyboard you can access that height like so:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == myTable)
return 150.0;
return tableView.rowHeight;
}
tableView.rowHeight will be the height that you set in the storyboard
You should make your view controller delegate of all 3 tableviews and in heightForRowAtIndexPath check which table called the method:
So in viewDidLoad add:
myTable1.delegate = self;
myTable2.delegate = self;
myTable3.delegate = self;
and then:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == myTable1) {
return 150.0;
}
else if(tableView == myTable2) {
return 75.0;
}
else if(tableView == myTable3) {
return 75.0;
}
}
How about using the tag property? Set the tag for each table to a unique number then return height like this:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (tableView.tag == 1)
{
return 150;
}
return 250;
}

How to set height of cell in grouped style table?

I have made a table with two types of custom cells using interface builder:
Everything works fine, except lower cell's height is same as cell above it. It has everything i put - UIImageView etc...
So I was thinking if it is not possible to change cell height when table style is grouped?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 1) {
return 100;
}
return 44;
}
We can use UITableview Delegate Methods :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1)
{
return 60;
}
else
{
return 44;
}
}
You have to use this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75.0f;
}
it is use full for me.

Fetching and displaying more records on UITableView

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..

iPhone - How add a final line in a UITableView to add items, preventing rearranging of this line

I have a UITableView put in edited mode.
self.tableView.editing = YES;
In this table view, I have some lines displayed in a custom cell, and I'd like to add one at the end that would allow the user to add an item (using another view).
So I wrote some lines :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return "number of lines" + 1;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row+1 != [tableView numberOfRowsInSection:0]) {
return UITableViewCellEditingStyleDelete;
}
else {
return UITableViewCellEditingStyleInsert;
}
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row + 1 == [tableView numberOfRowsInSection:0]) {
cell.textLabel.text = #"Add a line...";
}
else {
do the stuff in the custom cell
}
}
Doing this way, the UITableView allows to rearrange any line. I can move the first line after the "add line", and move the "add line" in first position.
How may I remove the arrange button on the "add line" cell and prevent other lines to go under this one ?
Or is there a better way to code this ?
Implement
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return ([indexPath row] != INDEX_OF_SPECIAL_ROW)
}
and
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if ([proposedDestinationIndexPath row] < NUMBER_OF_ROWS)
{
return proposedDestinationIndexPath;
}
NSIndexPath *otherPath = [NSIndexPath indexPathForRow:NUMBER_OF_ROWS-1 inSection:0];
return otherPath;
}
The much easier way would be to set a tableFooterView. You can place any UIView in there, so not only are you allowed to add a UITableViewCell but also a completely custom subclass. Doing it this way you will avoid all the unnecessary checks.
return UITableViewCellEditingStyleNone
For that row in - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
And check out – tableView:moveRowAtIndexPath:toIndexPath:

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;
}