Iphone:UITableView 标题行在 6 行后重复
Posted
技术标签:
【中文标题】Iphone:UITableView 标题行在 6 行后重复【英文标题】:Iphone : UITableView Header row repeating after 6 rows 【发布时间】:2012-05-04 14:32:45 【问题描述】:我正在开发一个通用应用程序。在这个应用程序中有一个 UITableView。问题是 TableView 中的标题行在 iphone 中的 6 行和 iPad 中的 9 行之后重复。
我用谷歌搜索但没有找到解决此标题问题的解决方案。请帮忙。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of rows in the section.
return [tripList count];
UiTableview cellForRowAtIndexPath 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UILabel *lblTripNumber = nil;
UILabel *lblTripState = nil;
UIImageView *imgTripState = nil;
UILabel *lblStateTitleText = nil;
UILabel *lblTripNUmberTitleValue = nil;
static NSUInteger const kTripNumberLabelTag = 2;
static NSUInteger const kTripStateLabelTag = 3;
static NSUInteger const kTripImageTag = 4;
BOOL isiPhone = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row == 0)
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (!isiPhone) // 1=Ipad
lblStateTitleText = [[[UILabel alloc] initWithFrame:CGRectMake(205, 27, 250, (cell.contentView.frame.size.height))] autorelease];
lblStateTitleText.font = [UIFont boldSystemFontOfSize:32];
else
lblStateTitleText = [[[UILabel alloc] initWithFrame:CGRectMake(65, 10, 130, (cell.contentView.frame.size.height))] autorelease];
lblStateTitleText.font = [UIFont boldSystemFontOfSize:16];
lblStateTitleText.textAlignment = UITextAlignmentLeft;
lblStateTitleText.backgroundColor = [UIColor clearColor];
lblStateTitleText.tag = 11;
lblStateTitleText.text = @"Trip State";
lblStateTitleText.textColor = [UIColor cyanColor];
[cell.contentView addSubview:lblStateTitleText];
if (!isiPhone) // 1=Ipad
lblTripNUmberTitleValue = [[[UILabel alloc] initWithFrame:CGRectMake(445, 27, 290, (cell.contentView.frame.size.height))] autorelease];
lblTripNUmberTitleValue.font = [UIFont boldSystemFontOfSize:32];
else
lblTripNUmberTitleValue = [[[UILabel alloc] initWithFrame:CGRectMake(180, 10, 250, (cell.contentView.frame.size.height))] autorelease];
lblTripNUmberTitleValue.font = [UIFont boldSystemFontOfSize:16];
lblTripNUmberTitleValue.textAlignment = UITextAlignmentLeft;
lblTripNUmberTitleValue.backgroundColor = [UIColor clearColor];
lblTripNUmberTitleValue.tag = 12;
lblTripNUmberTitleValue.text = @"Confirmation#";
lblTripNUmberTitleValue.textColor = [UIColor greenColor];
[cell.contentView addSubview:lblTripNUmberTitleValue];
else
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGFloat xIndex = 6.0;
CGFloat xIpadIndex = 6.0;
CGFloat TripNumberLabelWidth = 130.0;
if (!isiPhone) // 1=Ipad
imgTripState = [[[UIImageView alloc] initWithFrame:CGRectMake(xIndex, 18, 50, 64)] autorelease];
else
imgTripState = [[[UIImageView alloc] initWithFrame:CGRectMake(xIndex, 8, 32, 32)] autorelease];
imgTripState.tag = kTripImageTag;
[cell.contentView addSubview:imgTripState];
xIndex +=73;
xIpadIndex +=85;
if (!isiPhone) // 1=Ipad
lblTripState = [[[UILabel alloc] initWithFrame:CGRectMake(xIpadIndex, 25, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripState.font = [UIFont boldSystemFontOfSize:38];
else
lblTripState = [[[UILabel alloc] initWithFrame:CGRectMake(xIndex, 1, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripState.font = [UIFont boldSystemFontOfSize:20];
lblTripState.textAlignment = UITextAlignmentLeft;
lblTripState.backgroundColor = [UIColor clearColor];
lblTripState.tag = kTripStateLabelTag;
lblTripState.textColor = [UIColor colorWithRed:(0.0/255) green:(241.0/255) blue:(216.0/255) alpha:1.0f];
[cell.contentView addSubview:lblTripState];
xIndex +=132;
xIpadIndex +=120;
if (!isiPhone) // 1=Ipad
lblTripNumber = [[[UILabel alloc] initWithFrame:CGRectMake(xIpadIndex, 25, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripNumber.font = [UIFont boldSystemFontOfSize:35];
else
lblTripNumber = [[[UILabel alloc] initWithFrame:CGRectMake(xIndex, 0, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripNumber.font = [UIFont boldSystemFontOfSize:18];
lblTripNumber.textAlignment = UITextAlignmentLeft;
lblTripNumber.backgroundColor = [UIColor clearColor];
lblTripNumber.tag = kTripNumberLabelTag;
lblTripNumber.textColor = [UIColor greenColor];
[cell.contentView addSubview:lblTripNumber];
else
// A reusable cell was available, so we just need to get a reference to the subviews using their tags.
lblTripNumber = (UILabel *)[cell.contentView viewWithTag:kTripNumberLabelTag];
lblTripState = (UILabel *)[cell.contentView viewWithTag:kTripStateLabelTag];
// imgTripState = (UILabel *)[cell.contentView viewWithTag:kTripImageTag];
lblTripNumber.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
lblTripState.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
int indCount = indexPath.row -1;
TripDetails *trip = [tripList objectAtIndex:indCount];
lblTripNumber.text = trip.tripNumber;
lblTripState.text = trip.tripState;
// Configure the cell...
return cell;
【问题讨论】:
将viewForHeader的代码放入section和section的数量 【参考方案1】:您的 TableData 中可能只有 6 个项目。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [TableData count];
在您的情况下,“TableData”是“tripList”。
【讨论】:
【参考方案2】:我知道了,我删除了
if (cell == nil)
而且效果很好。实际上它是在第一个标题行中寻找它的标签,并且无法替换它的值,所以产生了问题。
【讨论】:
我也是这样做的,但是我的应用程序崩溃了【参考方案3】:在部分中检查您的 viewFor 标题。如果您将数组用于标题视图,则此数组对象未正确分配内存。它使用最后一个对象来完成它的所有发生并替换旧的。在向其中添加对象之前,先创建一个数组的内存分配对象。告诉我这是否是解决方案
【讨论】:
没错,因为您正在重用单元格,它们会向您显示已经填充的旧单元格。这就是为什么我说你应该将单元格的数量限制为数组中的对象数量,你应该没问题。【参考方案4】:您的代码尝试重用单元格,但您的编码方式是错误的。有关适当的模式,请参阅我的回答 here。简而言之,您应该只在if (cell == nil)
部分中定义共享单元格属性,行特定属性,如标签文本等应该在此部分之外定义。对于索引 0 处的行,情况并非如此。
【讨论】:
以上是关于Iphone:UITableView 标题行在 6 行后重复的主要内容,如果未能解决你的问题,请参考以下文章
uitableview 的 UIRefreshControl 仅在 iphone 6 中不起作用
在 IOS 8 中工作的具有动态高度的 UITableView 行在 ios 7 中不工作