gpt4 book ai didi

ios - indexpath.row 显示的行数较少

转载 作者:行者123 更新时间:2023-11-28 17:59:42 25 4
gpt4 key购买 nike

我创建了一个带有数组的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;
}

enter image description here

关于ios - indexpath.row 显示的行数较少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com