Tri*_*cky 160 iphone cocoa-touch objective-c uikit ios
我正在使用a UITableView
来布局内容'pages'.我正在使用表格视图的标题来布局某些图像等.如果它们没有浮动但我保持静态,但是当样式设置为时它们保持静态UITableViewStyleGrouped
.
UITableViewStyleGrouped
除了使用之外,有没有办法做到这一点?我想避免使用分组,因为它会在我的所有单元格中添加边距,并且需要禁用每个单元格的背景视图.我想完全控制我的布局.理想情况下,它们是"UITableViewStyleBareBones",但我没有在文档中看到该选项...
非常感谢,
sam*_*tte 318
一种可能更容易实现此目的的方法:
Objective-C的:
CGFloat dummyViewHeight = 40;
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
self.tableView.tableHeaderView = dummyView;
self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
迅速:
let dummyViewHeight = CGFloat(40)
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)
现在,节标题将像任何常规单元格一样滚动.
dav*_*d72 222
(对于因错误的表格样式而来到这里的人)将表格样式从普通变为分组,通过属性检查器或通过代码:
let tableView = UITableView(frame: .zero, style: .grouped)
Run Code Online (Sandbox Code Playgroud)
小智 72
警告:此解决方案实现了保留的API方法.这可能会阻止Apple批准App在AppStore上分发.
我已经描述了在我的博客中浮动部分标题的私有方法
基本上,您只需要子类化UITableView
并返回NO
其两个方法:
- (BOOL)allowsHeaderViewsToFloat;
- (BOOL)allowsFooterViewsToFloat;
Run Code Online (Sandbox Code Playgroud)
Sum*_*war 29
好吧,我知道它已经晚了但我不得不这样做.我现在花了10个小时寻找一个有效的解决方案,但没有找到完整的答案.确实找到了一些提示但很难让初学者理解.所以我不得不投入2美分并完成答案.
正如在少数答案中所建议的那样,我能够实现的唯一可行解决方案是在表格视图中插入普通单元格并将它们作为截面标题处理,但实现它的更好方法是将这些单元格插入每个部分的第0行.这样我们就可以非常轻松地处理这些自定义非浮动标头.
所以,步骤是.
使用UITableViewStylePlain样式实现UITableView.
-(void) loadView
{
[super loadView];
UITableView *tblView =[[UITableView alloc] initWithFrame:CGRectMake(0, frame.origin.y, frame.size.width, frame.size.height-44-61-frame.origin.y) style:UITableViewStylePlain];
tblView.delegate=self;
tblView.dataSource=self;
tblView.tag=2;
tblView.backgroundColor=[UIColor clearColor];
tblView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
Run Code Online (Sandbox Code Playgroud)像往常一样实现titleForHeaderInSection(您可以使用自己的逻辑获取此值,但我更喜欢使用标准委托).
- (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *headerTitle = [sectionArray objectAtIndex:section];
return headerTitle;
}
Run Code Online (Sandbox Code Playgroud)像往常一样实现numberOfSectionsInTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int sectionCount = [sectionArray count];
return sectionCount;
}
Run Code Online (Sandbox Code Playgroud)像往常一样实现numberOfRowsInSection.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount = [[cellArray objectAtIndex:section] count];
return rowCount +1; //+1 for the extra row which we will fake for the Section Header
}
Run Code Online (Sandbox Code Playgroud)在heightForHeaderInSection中返回0.0f.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.0f;
}
Run Code Online (Sandbox Code Playgroud)不要实现viewForHeaderInSection.完全删除方法而不是返回nil.
在heightForRowAtIndexPath中.检查是否(indexpath.row == 0)并返回节标题的所需单元格高度,否则返回单元格的高度.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
return 80; //Height for the section header
}
else
{
return 70; //Height for the normal cell
}
}
Run Code Online (Sandbox Code Playgroud)现在在cellForRowAtIndexPath中,检查是否(indexpath.row == 0)并根据需要实现单元格标题,并将选择样式设置为none.ELSE实现您想要正常单元格的单元格.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SectionCell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SectionCell"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone; //So that the section header does not appear selected
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SectionHeaderBackground"]];
}
cell.textLabel.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:indexPath.section];
return cell;
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray; //So that the normal cell looks selected
cell.backgroundView =[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellBackground"]]autorelease];
cell.selectedBackgroundView=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedCellBackground"]] autorelease];
}
cell.textLabel.text = [[cellArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row -1]; //row -1 to compensate for the extra header row
return cell;
}
}
Run Code Online (Sandbox Code Playgroud)现在实现willSelectRowAtIndexPath并返回nil,如果indexpath.row == 0.这将关心didSelectRowAtIndexPath永远不会被截面标题行触发.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
return nil;
}
return indexPath;
}
Run Code Online (Sandbox Code Playgroud)最后在didSelectRowAtIndexPath中,检查是否(indexpath.row!= 0)并继续.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row != 0)
{
int row = indexPath.row -1; //Now use 'row' in place of indexPath.row
//Do what ever you want the selection to perform
}
}
Run Code Online (Sandbox Code Playgroud)有了这个,你就完成了.您现在拥有一个完美滚动的非浮动节标题.
fra*_*yer 28
您应该可以通过使用自定义单元格来执行标题行来伪装它.然后,它们将像表视图中的任何其他单元格一样滚动.
您只需要在您cellForRowAtIndexPath
的标题行中添加一些逻辑以返回正确的单元格类型.
您可能必须自己管理您的部分,即将所有内容放在一个部分中并伪造标题.(您也可以尝试返回标题视图的隐藏视图,但我不知道这是否有效)
ada*_*ton 17
关于UITableViewStyleGrouped的有趣之处在于tableView将样式添加到单元格而不是TableView.
该样式作为backgroundView添加到单元格中,作为一个名为UIGroupTableViewCellBackground的类,它根据该部分中单元格的位置处理绘制不同的背景.
所以一个非常简单的解决方案是使用UITableViewStyleGrouped,将表的backgroundColor设置为clearColor,并简单地替换cellForRow中单元格的backgroundView:
cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease];
Run Code Online (Sandbox Code Playgroud)
Aks*_*Aks 14
更改TableView样式:
self.tableview = [[UITableView alloc] initwithFrame:frame style:UITableViewStyleGrouped];
Run Code Online (Sandbox Code Playgroud)
根据UITableView的苹果文档:
UITableViewStylePlain-普通表视图.任何节页眉或页脚都显示为内联分隔符,并在滚动表视图时浮动.
UITableViewStyleGrouped-一个表视图,其截面显示不同的行组.节标题和页脚不会浮动.
希望这个小改变能帮到你..
获得所需内容的最简单方法是将表格样式设置为UITableViewStyleGrouped
,分隔符样式为UITableViewCellSeparatorStyleNone
:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN; // return 0.01f; would work same
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
Run Code Online (Sandbox Code Playgroud)
不要尝试返回页脚视图,因为nil
在你必须得到你想要的之后不要忘记设置标题高度和标题视图.
小智 5
还有另一种棘手的方法.主要思想是将节号加倍,第一个只显示headerView,而第二个显示真实单元格.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionCount * 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section%2 == 0) {
return 0;
}
return _rowCount;
}
Run Code Online (Sandbox Code Playgroud)
那么需要做的是实现headerInSection委托:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section%2 == 0) {
//return headerview;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section%2 == 0) {
//return headerheight;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这种方法对您的数据源也几乎没有影响:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int real_section = (int)indexPath.section / 2;
//your code
}
Run Code Online (Sandbox Code Playgroud)
与其他方法相比,这种方式是安全的,同时不改变tableview的框架或contentInsets.希望这可能有所帮助.
对于斯威夫特 3+
只需使用UITableViewStyleGrouped
以下命令并将页脚的高度设置为零:
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
107436 次 |
最近记录: |