How to Handle CellForRowAtIndexpath method - iphone

I had one table view in which I had 15 row in one section.
so when the table view get loaded it shows only first 9 rows, I checked it with method CellForRowAtIndexpath method but I want to get all 15 rows to be get called in this method for the viewload method.
please help me on out.
thanks in advance....

it depends how much rows you set from
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if you have an array then return
[arr count]; as number of rows and if you want static rows then use return 15; in this method

You use these method or if you want all your cell on front of first time view then you should decrease the height of the cell using last method.And also check your array how much values he have.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [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:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier]
autorelease];
}
cell.textLabel.text=[Array objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;//use this method for cell height
}

Related

Error returning cell from tableView

I am relatively new to using table view in ios. I am trying to edit data using different view and update the values from original view. I set cell identifier and wrote following code.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}
But I get the following error:
NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
You need to check and make sure dequeueReusableCellWithIdentifier was able to dequeue a cell. It's crashing because it doesn't return a cell every time. If you were unable to dequeue a reusable cell you need to create a new one. Your code should look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}

How to reload UITableView step by step?

I am working on an application and i have to show 10 records in table view one time then if user want to see more records then user will have to click on "see more records" cell, then user will be able to see more records.
If any one know about this then please give me solution.
Thanx
This is simple prototype of what you can do:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([self.tableData count]>window) {
return window+1;
}
else{
return [self.tableData 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];
}
if (indexPath.row<window) {
cell.textLabel.text=[self.tableData objectAtIndex:indexPath.row];
}
else{
cell.textLabel.text=#"LoadMore";
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==window) {
window=window+step;
[tableView reloadData];
}
}
table data is an array that contains data that you want to show in tableview, window is number of lines to show initially and is used to set range of what to show, step is number of new items to add in every new load

how to remove all rows of a tableView

Can anyone tell me that how to remove all rows of a tableView
Clear the datasource (for example [dataSourceMutableArray removeAllObjects]) and reload the table ([tableView reloadData])
I think you can remove all values in dataSource of tableView? Then reload the tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 0;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
or specify return nil; instead of return cell; in cellForRowAtIndextPath function
or you can remove all values in array in cellForRowAtIndexPath like this
- (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.
// remove below line
//cell.textlabel.text=[array objectAtIndex:indextPath];
return cell;
}

Auto scrolling UITableView Problem

i am using below code for auto scrolling UITableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [DataArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[CustomTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
if(isAutoScrollEnabled)
[DataTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
cell.Time.text = [[DataArray objectAtIndex:indexPath.row]objectForKey:#"CurrentTime"];
cell.SerialNo.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
return cell;
}
But it fluctates my tableview every time i reload data in tableview. Is there any solution ?
Can anyone help me ?
Thanks in advance......
What do you want?
smooth scrolling to down?
or
scrolling to new cell position?
first case(smooth scrolling) should use NSTimer.
second case(to new cell position) should override UITableView[updateData]
I'm not sure exactly what you are saying. But I will say that calling scrollToRowAtIndexPath in your cellForRowAtIndexPath looks like it could cause problems. What you are doing is telling it to scroll each time it generates a cell. That doesn't look right.
I also have to wonder if you really want to be animating that scrolling.

iPhone UITableView with Multiple Sections

Apologies if this is a simple question but googling hasn't been able to help me.
I am planning to display 3 arrays of data in a Table View that has each array in a different section in the Table View.
Can any one provide me with a link to a good tutorial or sample code that could help me with this?
Once again I apologize if this is a simple question and answer but I am a newbie to Xcode and working on my first serious project for it.
Thanks!
In your TableViewController implement:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self getDataArray:section]count];//implement getDataArray
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSObject *data = [[self getDataArray:indexPath.section]objectAt:indexPath.row];//implement getDataArray
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//add Code to populate cell with data
}
return cell;
}
Other methods to probably implement:
- (UIView *)tableView: (UITableView *)tableView viewForHeaderInSection: (NSInteger)section
- (CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section