[英]First UITableViewCell should be fixed for static UITableView in UITableViewController
我有一个带有4个自定义单元格的静态UITableView
。 由于单元格是静态的,因此它表示整个事情都在UITableViewController
。 我在单元格中放入一些随机图像。 现在,我想将我的第一个单元格固定在tableView的顶部。 其余三个单元格将滚动。 我不能通过在UITableView
顶部放置一个额外的UIImageView
来做到这一点,因为它是静态单元格。 那我该怎么办呢?
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ((indexPath.row)==0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"one"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:@"default-cover-image.jpg"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==1)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"two"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:@"sectionHeader.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==2)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"three"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:@"sectionHeader2.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==3)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"four"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:@"default-cover-image.jpg"];
[cell.contentView addSubview:backgroundView];
}
return cell;
}
使单元格与其他单元格一起滚动,如果您具有纯样式表视图,则可以简单地使用该部分的标题。
如果您只有一节,则所有单元格都将在标题视图下滚动。
要进行简单的实验,只需添加以下代码
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
并使其返回一个字符串。 如果需要更多自定义,请使用以下委托方法:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
我习惯于设置另一个包含一个UIImageView和一个UITableViewController的UIViewController。 就像下面这样:
您可以在对象库中选择“查看容器”,就像这样:
运行后,就像这样:
为了完成您所描述的,您有2个选择。
如果您使用storyboards
这非常简单。 只需在tableView
上方添加所需单元格高度的视图,然后使用自动布局将它们保持在一起,就像这样:
您可以看到下面的黄色视图,它的作用类似于tableView
的标题,但是一旦用户向下滚动表,它就不会向下滚动
如果不使用storyboard
,则必须通过代码创建视图,或为tableView创建标题。 标头的唯一问题是,如果用户向下滚动,标头将向下滚动
我不知道有没有可能。 但是我找到了另一种解决方案来完成任务。 在这里,静态单元格像往常一样滚动,但是我在tableView
顶部放了一些额外的空间,并将UIImage
放置在该空白位置。
使用CGRectMake(0, 170, 320, 960)
设置我的静态tableView的框架:
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
它在该空白区域中放置了一些黑色空间。 所以我把背景设为白色。
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
然后在该空白处添加一个UIImageView
(在这里您不能在tableView
添加UIImage
,而必须在superView
添加该图像)。
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:@"default-cover-image.jpg"];
[self.view.superview addSubview:backgroundView];
因此,代码将如下所示:
- (void)viewDidAppear:(BOOL)animated
{
// Set the frame of the UITableViewController table
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
// Set the image in superView (not in tableView) as subView on top of the tabelView
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:@"default-cover-image.jpg"];
[self.view.superview addSubview:backgroundView];
}
尝试使用普通单元格并将其用作标题,而其他单元格则将它们加索引+1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayPeople count]<1 ? 0 : arrayPeople.count-1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(arrayPeople.count>0)
{
RankingCell *cell =nil;
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RankingCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[RankingCell class]])
{
cell = (RankingCell *)currentObject;
break;
}
NSDictionary *element=[arrayPeople objectAtIndex:0];
element=[element dictionaryByReplacingNullsWithStrings];
[cell setupCell:[element objectForKey:@"username"] title2:[element objectForKey:@"total"] title3:[element objectForKey:@"place"] remoteImage:[element objectForKey:@"image"] forPath:[NSIndexPath indexPathForItem:0 inSection:0]];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"RankingCell";
RankingCell *cell =(RankingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RankingCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[RankingCell class]])
{
cell = (RankingCell *)currentObject;
break;
}
}
NSDictionary *element=[arrayPeople objectAtIndex:indexPath.row+1];
element=[element dictionaryByReplacingNullsWithStrings];
[cell setupCell:[element objectForKey:@"username"] title2:[element objectForKey:@"total"] title3:[element objectForKey:@"place"] remoteImage:[element objectForKey:@"image"] forPath:[NSIndexPath indexPathForItem:indexPath.row+1 inSection:0]];
return cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.