Auto scrolling UITableView Problem - iphone

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.

Related

TableView cell for row at index path is not beeing called every time

I have created a class to generate tickets and when the ticket is generated I want to show it in the table view one by one.
I have created a protocol for that class and once the ticket is prepared I send a message to its delegate which is tableView to reload the table view.
when the reload method is called on the tableView - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is being called every time a new ticket is generated but the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called every time the ticket is generated but once all the tickets are generated it is being called
below is the code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.ticketGenerator == nil) {
return 0;
}
else{
return self.ticketGenerator.ticketNumbers.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
HSEticketView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[HSEticketView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell Tickets:[self.ticketGenerator.ticketNumbers objectAtIndex:indexPath.row]];
}
// Configure the cell...
return cell;
}
//to increase the height of the cell
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 135;
}
-(void)background
{
[self.ticketGenerator GenerateNoOfTickets:[self.enteredNo.text intValue]:self];
}
- (IBAction)done:(id)sender {
[self.enteredNo resignFirstResponder];
self.enteredNo.hidden = YES;
self.label.hidden = YES;
self.button.hidden = YES;
self.tableView.hidden = NO;
[self performSelectorInBackground:#selector(background) withObject:nil];
NSLog(#"dgf");
}
#pragma ticketGenrate delgate methods
-(void)generationOfTicketCompleated
{
[self.tableView reloadData];
}
All changes to the UI must be done on the main thread. If you execute
[self.ticketGenerator GenerateNoOfTickets:...]
on a background thread and (as I assume) that functions calls
[tableView insertRowsAtIndexPaths:...]
that will not work, because insertRowsAtIndexPaths must be called on the main thread.
If you want to update the table view from a background thread, you can for example do
dispatch_async(dispatch_get_main_queue(), ^{
add item to data source array ...;
[tableView insertRowsAtIndexPaths:...];
});
check your return self.ticketGenerator.ticketNumbers.count; is 0 then cellForRowAtIndexPath is not call or reload your tableview in ViewWillAppear like [reload YourTableViewName].
Thank u Martin
the problem is I am doing the [tableView reloadData] on another thread as uielements need to be run on main thread the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is not being called the solution i got is
-(void)generationOfTicketCompleated
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}

How to Handle CellForRowAtIndexpath method

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
}

UITableView action row

On the WWDC 11 video 'session 125 - UITableCiew Changes, Tips, Tricks' at 24:30+ Mr Luke Hiesterman is giving a demo which adds a cell to the table view when a cell is selected.
I want to add that functionality to my IOS application, but I can't figure out how to make it.
Some code is not shown in the demo-video. And there is no downloadable source of the demo.
Can anyone help me out?
EDIT:
I can add a new row below the selected row but it is an other custom cell.
(I have a list of contracts which you can accept)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ((indexPath.row >= [_contracts count]) ? 40 : 60);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_contracts count] + ((_addedRow) ? 1 : 0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseID = #"contract_cell";
ContractTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if(!cell)
{
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ContractTableViewCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"contract_cell.png"]]];
}
NSLog(#"row?: %d", indexPath.row);
//I thought this would work... not.
if((indexPath.row >= [_contracts count]))
{
[cell.label setText:#"new"];
}
else
{
Contract *contract = [_contracts objectAtIndex:indexPath.row];
[cell.label setText:#"test"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!_addedRow)
{
_addedRow = YES;
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
}
For example I press the first row. A row is added below the first one. But now the cellForRowAtIndexPath.. is called for row 2 (needs to be the new custom)
how can I check if it is a new one?
You need to remember which of your rows is selected to show the added row at the correct index. Currently you're always assuming that the added row is the very last one because you're configuring it in tableView:cellForRowAtIndexPath: only if the index is bigger than your number of contracts.
Say you have five contracts and the third one is selected. if((indexPath.row >= [_contracts count])) is only true for the last row but actually want this condition to be true for the fourth row, so it should be if (indexPath.row == selectedRowIndex + 1) (you need to store the selected row index in some instance variable).
This answer should help you out. It tells you how to put your UITableViewController into "update mode" and then lets you insert new rows (even with animation): UITextField in UITableViewCell - adding new cells

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

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