in the method below why indexPath.Row always return 0???
- (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.
[cell.textLabel setText:[mainItems objectAtIndex:indexPath.row]];
return cell;
}
mainItems is a NSMutableArray and has 5 items ("item 1", "item 2",...) the View does get 5 items... but they are all "Item 1" and NOT item 1, item 2...
what am i doing wrong?
My guess is that you're returning the wrong value in your implementation of -numberOfSectionsInTableView, and the table view is asking you for the 0th row of 5 sections.
Table views have sections, and each section has rows. Many table views only have one section, but many rows in that section. It sounds like you have one section, with 5 rows. Be sure that you return the correct values in each of:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Related
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *cellIdentifier = #"Cell";
customcell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[customcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.finacialyear.text=[self->entries1 objectAtIndex:indexPath.row];
cell.term.text=[self->entries2 objectAtIndex:indexPath.row];
cell.balance.text=[self->entries3 objectAtIndex:indexPath.row];
cell.tintColor = [UIColor blueColor];
return cell;
}
have to add the rows values in the tableview cell and display in to the final row.can anyone suggest me how to do this in objective c.
thanks
First you need to add one extra row:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#rows you have now#> + 1;
}
Then you should return a different cell if you are in the last row:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isLastRow) {
return BalanceSumCell;
} else {
return customcell;
}
}
Remember to name your classes with a capital first letter (CustomCell and not customcell).
If you want to do different calculations when the user selects a cell, you should do that in -didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Keep track of user's selection and update the last cell as needed
}
In general, your question is broad and if you break it down to smaller problems you will find a lot of related answers to help you.
Good luck!
i m quite stucking with this problem. i have a NSArray *list holding 70 elements from a plist file. now i need to populate them to UITableView.
what i want is to display 20 items at a time, then click the 21st cell to display another 20.. then press the 41st cell to display another 20 items... and so on till all 72 cells reveal..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
static NSString *addCellID = #"addMoreCell";
UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:addCellID];
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"labelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
} else if (indexPath.row < 25){
NSDictionary *dic = [self.list objectAtIndex:indexPath.row];
cell.textLabel.text = [dic objectForKey:#"Title"];
cell.detailTextLabel.text = [dic objectForKey:#"Tips"];
return cell;
} else {
if (cell == nil) {
addCell.textLabel.text = #"Load More...";
return cell;
}
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 20)
{
[self.tableView reloadData];
}
}
Take an array, in which you will add 20 elements from plist, and
set numberOfRowsInSection = [array count] and
check if numberOfRowsInSection < totalNumberOfRecords in plist, if it is less, then increment numberOfRowsInSection by 1.
And in cellForRowAtIndexPath
check if (indexPath.row < totalNumberOfRecords -2 && indexPath.row < numberOfRowsInSection-1)
then add "LoadMore Cell" else not.
So I do something quite similar. You have an array with 70 strings (or dictionaries) to show in the table. The 'numberOfRowsInSection:' should be using an ivar which is initially set to 20.
When your button gets pressed, you increase the ivar by 20 (but to no more than the actual amount of data!), you reload the table, then you can scroll the table up to the "new" cell using tableView methods.
In UITableView we have headers
For example
Say I want to group businesses by the building they are in.
BuildingA 10
Business1
Business2
Business3
Business4
How do I arrange so that the 10 is on the right side of the header
You can override tableView:viewForHeaderInSection: to add your own extra label whose rect is somewhere on the right side, like CGRectMake(280,5,30,20);. You should also set it so the text aligns right.
Note that if you do this, you also have to provide the left-side label (since it is no longer there) and you must override tableView:heightForHeaderInSection: as well.
In that u can use the custom header cell then that cell attach in the header.
for that following code is implement.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 2;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v=[[[NSBundle mainBundle] loadNibNamed:#"tHeader" owner:self options:nil] objectAtIndex:0];
[(UILabel*)[v viewWithTag:1] setText:[NSString stringWithFormat:#"%i",section+1]];
return v;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (section==0)?5:((section==1)?7:((section==2)?3:4));
}
// Customize the appearance of table view cells.
- (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];
}
cell.textLabel.text=[NSString stringWithFormat:#" Hello-%i",indexPath.row];
// Configure the cell.
return cell;
}
download source from here.
I would like to replace the cell that is touched by a custom cell. I do this by calling reloadRowsAtIndexPaths
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row selected: %d", [tableView indexPathForSelectedRow] row]);
NSLog(#"Section selected: %d", [tableView indexPathForSelectedRow] section]);
//return a custom cell for the row selected
}
When I try to access/log the indexPathForSelectedRow from within the cellForRowAtIndexPath, it returns 0,0 no matter which cell I select. Why is this?
Your call to reloadRowsAtIndexPaths: will cause the table view to reload the given cells and therefore cause the table to no longer have a selected row at that position. The cellforRowAtIndexPath method is a request from the data source to provide a row for the given index path. If you need to determine if a cell was selected prior to the request, you could store the indexPath of the selected row in a member. Then check the member in your cellForIndexPathMethod.
The following code sample assumes you are using ARC for memory management.
- (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];
}
cell.textLabel.text = [NSString stringWithFormat:#"Cell %d_%d", indexPath.section, indexPath.row];
// Configure the cell...
if(selectedIndexPath != nil) {
NSLog(#"Selected section:%d row:%d", selectedIndexPath.section, selectedIndexPath.row);
//TODO:Provide a custom cell for the selected one.
//Set the selectedIndexPath to nil now that it has been handled
selectedIndexPath = nil;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Store the selected indexPath
selectedIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
i am using UITableView with the following delegates.
tableView.delegate = self;
tableView.dataSource=self;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;enter code here
}
it will set the number of Rows in a section at the loading time.
it will trigger the following delegate and i can do whatever i need with table cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
However i need to create/reset the table rows dynamically(on a button click) and need to trigger
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
this delegate to set some datas to table cell.
how can i call this delegate in a button click.and also i need to reset the total rows of my table on the same button click at each time.
can any one help me to do it.
[self.tableView reloadData];
And they all lived happily ever after.
You need to change the dataSource of your table view and then call:
[tableView reloadData];
so in a button action you should:
-(void)buttonClicked{
tableViewArray = ...//load array with new data;
[self.tableView reloadData];
}
Have to set:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
**return [tableViewArray count];**
}
- (void)buttonClicked
{
tableViewArray = ... //load array with new data;
[self.tableView reloadData];
}