具有不显示静态单元格的背景的子类UITableView



我正在尝试制作一个处理用户登录的视图控制器。由于我需要视图控制器是可滚动的,包含一个单独的视图(用于登录),并包含一个背景,所以我决定采用制作tableviewcontroller的方法,将其子类化,然后添加必要的视图。我将UITableViewController子类化,并将此代码添加到viewdidload()中

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableViewControllerBlurred.png"]];
[tempImageView setFrame:self.tableView.frame];

self.tableView.backgroundView = tempImageView;
[tempImageView release];

这成功地将我的背景图像添加到了控制器中,此时,视图控制器看起来像:https://i.stack.imgur.com/N0TJO.jpg

接下来,我开始使用静态单元格,将视图放入其中一个单元格,并开始设计登录屏幕。在这一点上,我的故事板看起来像:http://imgur.com/n6GKeGq&ST4H8uf,但问题是在我运行项目时出现的。

当我运行这个项目时,我会得到与第一张图片中相同的背景屏幕,没有任何新的静态单元格或视图。对于这个问题的原因,我们非常感谢所有的帮助。非常感谢。

CellForRowAtIndexPath代码:

*/
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
// Configure the cell...
return cell;
}
*/

如果您想要的是一个只有静态单元格的UITableView,那么请学习将UIScrollView与UIViewController一起使用。

@interface vc : UIViewController
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation vc
- (id)init // or whatever initializer you are using to make your view controller
{
    self = [super init];
    if (self) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,568)];
        [_scrollView setContentSize:CGSizeMake(320,568)];  // equals one screen
        [_scrollView setContentSize:CGSizeMake(320,568*2)];  // equals two screens, etc
        // contentSize property determines how much you can scroll inside the UIScrollView view if that makes any sense to you.
        [self.view addSubview:_scrollView]
        // one way of adding a background
        UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
        [self.view addSubview:backgroundImageView];
        [_scrollView addSubview:[self newStaticCellAtPosition:CGRectMake(0,0,320,45)]];
        [_scrollView addSubview:[self newStaticCellAtPosition:CGRectMake(0,45,320,45)]];
        // add subviews, you can even use UITableViewCell if you want.
        // I'd use simple UIView's and draw separators and whatnot myself if I were you.
    }
    return self;
}
- (UIView *)newStaticCellAtPosition:(CGRect)position
{
    UIView *staticCell = [[UIView alloc] initWithFrame:position];
    [staticCell setBackgroundColor:[UIColor redColor]];
    return staticCell;
}
@end

对于其他属性,您应该查看UIScrollView文档。请记住,UITableView继承自UIScrollView,因此如果可以轻松选择所需内容。

必须设置表视图的第一个检查数据源和委托。因此,你可能会遇到问题。

永远不要使用UITableViewController!在我遇到的几乎所有情况下,使用UIViewController和添加表视图都要容易得多。您根本无法访问UITableViewController的backgroundView并使其正确滚动。我意识到,你只能用UITableViewController创建一个"静态"表视图,但它足够简单,可以模仿与常规表视图完全相同的行为,而且你不必处理无法在表后面添加视图(如背景图像)的头痛问题。

最新更新