这是我的cellForRowAtIndexPath代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"MonthViewPopUpCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
cell = [nibObjcet objectAtIndex:0];
cellTitle.font = [UIFont fontWithName:fontB size:size4];
cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
}
if (indexPath.row == 0) {
cellTitle.text = @"11:03";
}
return cell;
}
由于某些原因我不明白,tableview错误地重用了我的自定义单元格。我以为这个代码会让第一个单元格显示11:03而所有其余的都会显示10:00(如xib文件中),但是其他一些单元格也会显示11:03,当我滚动时它们的位置会改变像疯了一样上下......
有人能告诉我我做错了吗?
由于
答案 0 :(得分:0)
它不会在同一个位置重复使用相同的单元格,它只需要一个已经存储在内存中的单元格,并将其重新用于另一个位置的表格,因此您需要每次都设置它的文本,因为例如:
if (cell == nil) {
NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
cell = [nibObjcet objectAtIndex:0];
cellTitle.font = [UIFont fontWithName:fontB size:size4];
cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
}
if (indexPath.row == 0) {
cellTitle.text = @"11:03";
}
else{
cellTitle.text = @"10:00";
}
答案 1 :(得分:0)
通常,一旦移出UITableView的可见区域,单元格就会被重用。你可以做一件事。还要检查nib是否将自定义单元格的可重用标识符设置为 MonthViewPopUpCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"MonthViewPopUpCell"; ////As you have specified in XIB
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
cell = [nibObjcet objectAtIndex:0];
cellTitle.font = [UIFont fontWithName:fontB size:size4];
cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
}
if (indexPath.row == 0) {
cellTitle.text = @"11:03";
}else {
cellTitle.text = @"10:00";
}
return cell;
}
答案 2 :(得分:0)
以下是调用自定义单元格的好方法。我希望它能奏效。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"cell";
MonthViewPopUpCell *cell = (MonthViewPopUpCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjcet = [[NSBundle mainBundle] loadNibNamed:@"MonthViewPopUpCell" owner:self options:nil];
cell = [nibObjcet objectAtIndex:0];
cellTitle.font = [UIFont fontWithName:fontB size:size4];
cellSubtitle.font = [UIFont fontWithName:fontR size:size4];
}
if (indexPath.row == 0) {
cellTitle.text = @"11:03";
}else{
cellTitle.text = @"10:00";
}
return cell;
}