我有一个静态UITableView
,有4个自定义单元格。由于单元格是静态的,所以它指的是整个单元格在UITableViewController
中。我在细胞内放了一些随机图像。现在我想把我的第一个单元格固定在tableView的顶部。剩下的三个单元格将滚动。我无法通过UIImageView
的额外UITableView
顶部来实现,因为它是静态单元格。然后,我该怎么做?
这是我的代码:
- (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;
}
答案 0 :(得分:3)
使单元格与其他单元格一起滚动,如果您有一个普通样式表格视图,则可以只使用该部分的标题。
如果您有一个部分,则所有单元格都将在标题视图下滚动
要进行简单的实验,只需添加此代码
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
然后让它返回一个字符串。如果您需要更多自定义,请使用此委托方法:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
答案 1 :(得分:2)
我习惯于设置另一个包含一个UIImageView和一个UITableViewController的UIViewController。就像下面这样:
您可以在对象库中选择View Container,就像那样:
跑步后,就像这样:
答案 2 :(得分:1)
要完成你所描述的内容,你有两个选择。
如果您使用storyboards
,则非常简单。只需在tableView
上方添加一个视图,其中包含所需单元格的高度,并使用autolayout将它们保持在一起,如下所示:
你可以看到黄色的下部视图,它的作用类似于tableView
的标题,但是一旦用户滚动表格就不会向下滚动查看
如果您不使用storyboard
,则必须按代码创建视图,或者为tableView创建标题。标题的唯一问题是,如果用户向下滚动,它们将向下滚动
答案 3 :(得分:0)
我不知道是否有可能。但我找到另一种解决方案来完成任务。在这里,静态单元格像往常一样滚动,但是我在tableView
顶部放了一些额外的空格,并将UIImage
放在那个空位。
使用CGRectMake(0, 170, 320, 960)
设置我的静态tableView的框架:
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
它在那个空的空间里放了一些黑色的空间。所以我把背景设为白色。
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
然后在该空白处添加UIImageView
(此处您无法在UIImage
中添加tableView
,您必须在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];
}
答案 4 :(得分:0)
尝试使用普通单元格并将其用作标题,而另一个将其编入索引+ 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;
}