我创建了一个带有数组的tableview
。我的数组中有 7 个项目。但是在 cellForRowAtIndexPath
方法中,我无法获得 indexpath.row == 6
。可能是什么原因。
代码如下。
-(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;
}
我正在关注:
[这里] ( https://www.dropbox.com/s/pz6t34xr7l4glxz/menu1.png )
第一个索引覆盖在最后一个上。
最佳答案
细胞被重复使用。
将您的日志语句放在 if(cell=nil)
之外:
这是在 TableView 中输出数组内容的工作示例。数组中第七个索引为 6 的项目(“topUser”)为红色。
-(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;
}
关于ios - indexpath.row 显示的行数较少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201749/