Reputation: 4436
I have created an tableview
with an array. There are 7 items in my array. But in cellForRowAtIndexPath
method i'm unable to get indexpath.row == 6
. What could be the reason.
Code is below for this.
-(void)viewDidLoad
{
[super viewDidLoad];
self.menu = [NSArray arrayWithObjects:@"Home",@"ansCategory",@"askQue",@"myQue",@"likedQue", @"topQue", @"topUser",nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
// NSLog(@"count::::::%d",self.menu.count);
return self.menu.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
NSLog(@"rows:%@",indexPath);
NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);
}
return cell;
}
I'm getting following out put:
[here] (https://www.dropbox.com/s/pz6t34xr7l4glxz/menu1.png)
first index is overlaying on last one.
Upvotes: 0
Views: 3113
Reputation: 4436
I changed my code to following line and it worked.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
Upvotes: 2
Reputation: 1332
The cell is reused.
Put your log statement outside of the if(cell=nil)
:
Here is the working example for outputting the contents of the array in a tableview. Item with index 6 which is the seventh in the array ("topUser") is in red.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSLog(@"rows:%@",indexPath);
NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);
cell.textLabel.text = self.menu[indexPath.row];
if (indexPath.row == 6)
cell.textLabel.textColor = [UIColor redColor];
return cell;
}
Upvotes: 5
Reputation: 1299
For N items the last indexpath is (N-1)
Thus, if you have 10 items, then the index path of last item is 9 because the indexpath of 1st item in array is 0.
indexPath of items starts with 0 and not 1.
Try to access the index path in your answer like below in your code
Instead of :
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
NSLog(@"rows:%@",indexPath);
NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);
}
Write
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
//Access the indexPath outside here...
//Because once the cells are created and not nil
//then the above scope will be bypassed.
NSLog(@"rows:%@",indexPath);
NSLog(@"row:%@",[self.menu objectAtIndex:indexPath.row]);
Upvotes: 3
Reputation: 20021
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
gets executed whenever the new row is needed to be created and added to the tableview.The tableview populates row such that when it needs to be dispayed on screen it is created by calling the very same method.So If your 7 th row is not visible on screen the method wont get executed for row 7 ie indexpath.row 6 and so to get the method to log the 7 th row just scroll down so that that cell can be made visible by the table and it will execute the method and you can have the log
Upvotes: 2