2014-08-29 35 views
0

我想做一个视图控制器处理用户登录。由于我需要视图控制器是可滚动的,包含一个单独的视图(用于登录),并且包含背景,我决定采用制作tableviewcontroller的子路径,并添加必要的视图。我子类的UITableViewController,并将此代码到viewDidLoad中()分类的UITableView与背景不显示静态单元

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

[tempImageView setFrame:self.tableView.frame]; 



self.tableView.backgroundView = tempImageView; 

[tempImageView release]; 

这成功添加我的背景图像控制器,在这一点上,视图控制器看起来像:http://imgur.com/ST4H8uf因为它应该。

接下来,我开始使用静态单元,将它放入视图中放入其中一个单元格中,并开始设计屏幕中的登录。在这一点上,我的故事板看起来像: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; 
} 
*/ 

回答

0

如果你想要的只是一个带有静态单元的UITableView,那么学习使用带有UIViewController的UIScrollView。

@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继承,所以如果很容易挑选你想要的东西。

+0

我将如何使用UIViewController实现UIScrollView? – 2014-08-29 08:24:51

+0

这里有一些代码让你开始,基本上只是创建一个UIScrollView对象属性,并将其添加到控制器视图,然后添加子视图。祝你好运! – believesInSanta 2014-08-29 08:43:54

+0

谢谢@believesInSanta只是还有一个问题......我会如何添加背景? – 2014-08-29 21:19:24

0

先检查数据源和tableview中的代表必须设置。 您可能会因为这个问题而陷入困境。

+0

它们已被设置... – 2014-08-29 08:20:17

0

切勿使用UITableViewController!在几乎所有我遇到的情况下,使用UIViewController并添加一个表视图要容易得多。你根本无法获取UITableViewController的backgroundView并使其正确滚动。我意识到你只能用UITableViewController创建一个“静态”表格视图,但它的简单性足以模拟与普通表格视图完全相同的行为,并且不必处理令人头痛的不能添加视图表格(如背景图片)。

+0

我会很高兴地使用一个视图控制器内部的表视图而不是tableviewcontroller。问题是,当我制作视图控制器时,添加与原型内容的表视图,我似乎无法弄清楚如何更改表视图的背景。有任何想法吗? – 2014-08-29 08:04:06

+0

我会做的是使用常规的UIViewController,添加一个表视图,然后在故事板中创建具有不同标识符的单元格。这模仿静态表视图的行为和工作原理是一样的... UITableViewController的问题是主视图是表视图,所以你基本上不能在它下面添加任何类型的背景。根据我的经验,像你这样的所有问题都来自于试图使UITableViewController工作。 – DBoyer 2014-08-29 08:11:16