这是我的表格视图的样子。
这是我的代码 当我向下滚动时会发生这种情况 的 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *items ;
}
@property (nonatomic,retain) NSMutableArray *items ;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize items;
- (void)viewDidLoad
{
[super viewDidLoad];
items = [[NSMutableArray alloc] init];
[items addObject:@"bla bla bla"];
[items addObject:@"rikifhdif bla bla"];
[items addObject:@"bla sdfkndskfnlk bla"];
[items addObject:@"sdf,nsdmf bla bla"];
[items addObject:@"sdjkfhksd bla bla"];
[items addObject:@"dkfhkusd bla bla"];
[items addObject:@"bla sdfjknskdjf bla"];
[items addObject:@"kiidrfjifig jjojdjfgdfgjjdjsjkhksidfl"];
[items addObject:@"mksjdhf ysdgjg bla"];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma tableview logic
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
{
return @"Title0";
}
else if(section == 1)
{
return @"Title1";
}
else if(section == 2)
{
return @"Title2";
}
if(section == 3)
{
return @"Title3";
}
else if(section == 4)
{
return @"Title4";
}
else
{
return @"Title5";
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return 3;
}
if (section==1) {
return 1;
}
if (section==2) {
return 2;
}
if (section==3) {
return 2;
}
if (section==4) {
return 1;
}
if (section==5) {
return 1;
}
return 1;
}
-(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 = [items objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
switch (indexPath.section) {
case 0:
if (indexPath.row==1) {
[[cell imageView] setHidden:YES];
}
if (indexPath.row==2) {
[[cell imageView] setHidden:YES];
}
break;
case 1:
if (indexPath.row==1) {
[[cell imageView] setHidden:YES];
}
if (indexPath.row==2) {
[[cell imageView] setHidden:YES];
}
break;
case 2:
if (indexPath.row==1) {
[[cell imageView] setHidden:YES];
}
if (indexPath.row==2) {
[[cell imageView] setHidden:YES];
}
break;
case 3:
if (indexPath.row==1) {
[[cell imageView] setHidden:YES];
}
if (indexPath.row==2) {
[[cell imageView] setHidden:YES];
}
break;
case 4:
if (indexPath.row==1) {
[[cell imageView] setHidden:YES];
}
if (indexPath.row==2) {
[[cell imageView] setHidden:YES];
}
break;
case 5:
if (indexPath.row==1) {
[[cell imageView] setHidden:YES];
}
if (indexPath.row==2) {
[[cell imageView] setHidden:YES];
}
break;
default:
break;
}
return cell;
//}
}
- (void)dealloc {
[items release], items = nil;
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
只想显示每个部分第一行的图像。加载时输出正常,但是当我滚动视图时,第一个图像消失。 我该如何解决这个问题?任何帮助,将不胜感激。非常感谢提前。
答案 0 :(得分:3)
当您向下滚动时,您的图片会消失,因为UITableView
会重复使用其单元格。比如说,对于第一部分的第1行,您要求提供单元格,并隐藏其图像
case 0:
if (indexPath.row==1) {
[[cell imageView] setHidden:YES];
}
现在稍后当您向下滚动时,UITableView
很明智,应该知道该细胞不再使用(因为它在屏幕上不再可见),并且重复使用细胞对于在视图下方出现的新行。因为你隐藏了图像,所以它是隐藏的。这里的关键是没有创建新的单元格,只重用旧单元格,并且它们的状态没有改变。
要解决此问题,只需添加row == 0
的案例:
case 0:
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else if (indexPath.row == 1) {
[[cell imageView] setHidden:YES];
}
else ...
我建议使用更清晰的语法:
case 0:
cell.imageView.hidden = indexPath.row != 0;
或者,如果您只想要所有的图像所有部分的第一行:
cell.imageView.hidden = indexPath.row != 0 // No need of case expression
答案 1 :(得分:2)
你已经为你的Index-Path.row设置了图片隐藏,如: -
例如第一部分,您将if (indexPath.row==1) { [[cell imageView] setHidden:YES];}
设置为与if (indexPath.row==2)
相同,这就是为什么您的第一部分仅显示第一个第0个索引行图像显示其他不显示。
case 0:
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else{
[[cell imageView] setHidden:YES];
}
只需使用我的cellForRowAtIndexPath
方法替换您的cellForRowAtIndexPath
代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row]; //uniq identifire for each section with it's row
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text = [items objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else {
[[cell imageView] setHidden:YES];
}
break;
case 1:
cell.textLabel.text = [items objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else {
[[cell imageView] setHidden:YES];
}
break;
case 2:
cell.textLabel.text = [items objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else {
[[cell imageView] setHidden:YES];
}
break;
case 3:
cell.textLabel.text = [items objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else {
[[cell imageView] setHidden:YES];
}
break;
case 4:
cell.textLabel.text = [items objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else {
[[cell imageView] setHidden:YES];
}
break;
case 5:
cell.textLabel.text = [items objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"Star.jpg"];
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else {
[[cell imageView] setHidden:YES];
}
break;
default:
break;
}
return cell;
//}
}
像这样创建CellIdentifier: -
NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row]; //uniq identifire for each section with it's row
答案 2 :(得分:0)
表格视图单元格出现时加载单元格,只需添加此代码所有情况0:10案例5:
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
我的sugession你为所有情况减少你的代码:
if (indexPath.row==0) {
[[cell imageView] setHidden:NO];
}
else {
[[cell imageView] setHidden:YES];
}
答案 3 :(得分:-1)
我通过进行以下更改解决了这个问题。但我不确定这是否正确。
//if(cell == nil)
//{
//}
我所做的是注释检查细胞是否为零的细胞条件。