我有一个包含各种条目的TableView。有时我需要向下滚动,有时只有几个条目,所以一切都可见。
我想在First Cell中设置一些设置和过滤器,但只有当用户向下滚动时才能看到它。
我的尝试:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
float rowHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];
self.tableView.contentOffset = CGPointMake(0, rowHeight);
}
和
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTopanimated:NO];
}
两者都不起作用。
对我的问题有任何建议吗?
答案 0 :(得分:1)
你在找这样的东西吗?
[self.tableView setContentInset:UIEdgeInsetsMake(t,l,b,r)];
这会以你想要的任何方式改变你的tableView
,但你绝对可以回滚。
答案 1 :(得分:0)
在TableView Delegate方法中尝试你的逻辑
- (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]; } if (indexPath.row == 0) { //Your filters here } else { //display the cell } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; }
此外,如果您不希望显示第一个单元格,只需写入
return nil; //for condition of indexpath.row == 0
并调整
中行的高度- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0 && isDisplayThisSection) {//first visible UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 25)] autorelease]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 10, 300, 25)]; [titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]]; titleLabel.text = @"Temp Test"; [titleLabel setBackgroundColor:[UIColor clearColor]]; [headerView addSubview:titleLabel]; [titleLabel release]; [headerView setBackgroundColor:[UIColor clearColor]]; return headerView; } return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease]; }
将值设置为0
希望这有帮助!!
答案 2 :(得分:-1)
在TableView Delegate方法中尝试你的逻辑
- (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]; } if (indexPath.row == 0) { //Your filters here } else { //display the cell } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; }
此外,如果您不希望显示第一个单元格,只需写入
return ; //for condition of indexpath.row == 0
并调整
中行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
将值设置为0
如果您希望像Pull_to_Refresh一样实现,最好在
中编写逻辑- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
希望这有帮助!!