1

我想显示每个单元格都包含嵌套 UIview 的表格视图,但我遇到了一些问题。

1) 当我尝试滚动表格视图时,我的应用程序崩溃。

通过将根视图而不是子视图添加到应用程序委托中来解决

现在问题2

2)我想在表格单元格中添加水平滚动视图。所以我可以在单个单元格中显示3个子视图,我可以在单元格中水平滚动..我该怎么做。我想做这个..

在此处输入图像描述

为了实现这一点,我有这个代码..

 - (id)initWithFrame:(CGRect)frame
  {
self = [super initWithFrame:frame];
if (self) {
    UIView *wagon = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 430, 187)];
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"wagon.png"]];
    wagon.backgroundColor = background;

    UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 830, 187)];
    scrollview.showsVerticalScrollIndicator=YES;
    scrollview.scrollEnabled=YES;
    scrollview.userInteractionEnabled=YES;

   UIView *videoview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 220 , 100)];

    UIImageView *video = [[UIImageView alloc]initWithFrame:CGRectMake(wagon.frame.origin.x+18, wagon.frame.origin.y+35, 220, 100)];
    UIImage *bgImage = [UIImage imageNamed:@"video.png"];
    video.image = bgImage;
    videoview.contentMode = UIViewContentModeLeft;
    [videoview addSubview:video];

    UIView *textview = [[UIView alloc]initWithFrame:CGRectMake(wagon.frame.origin.x+238,wagon.frame.origin.y+28, 150 , 187)];
    textview.contentMode = UIViewContentModeRight;
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(28,10, 150 , 87)];
    label.text=@"This is testing text for IOS app to check line are aligned or not and to check video image and nested views for UIviews";
    label.backgroundColor = [UIColor clearColor];
    label.textColor=[UIColor redColor];
    label.numberOfLines = 4;
    [textview addSubview:label];

    [wagon addSubview:textview];
    [wagon addSubview:videoview];
    [scrollview addSubview:wagon];
    [self addSubview:scrollview];       
  }
   return self;
   }

并从表格单元格中调用此视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"StateCell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Wagon1 *wg=[[Wagon1 alloc]init];
[cell addSubview:wg];
return cell;

}

通过添加 wg,我得到了一个火车 boggi,货车视图.. 我最后或第一个想要 2 个货车视图和引擎。

** 表崩溃问题已解决**

1)为了解决崩溃问题,我在stackoverflow上进行了搜索,我找到了解决方案,例如添加代表或更改为保留或强bla bla ..但这些都不适合我。这是我的代码。还有一件事我不使用 XIB ,笔尖或情节提要..

@interface MainScreenViewController : UIViewController 
{
UITableView* table;
 NSString * name;
 }
 @property (strong, retain) UITableView *table;
 @property (strong, retain) NSString *name;;
 @end

.m 文件

@interface MainScreenViewController ()
@end
@implementation MainScreenViewController
@synthesize table;
@synthesize name;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
{
self = [super initWithNibName:Nil bundle:Nil];
if (self) {
    // Custom initialization
}
return self;
 }
- (void)viewDidLoad
 {
[super viewDidLoad];
name=[[NSString alloc]init];
name=@"testing of row";
CGRect frame = self.view.frame;
table= [[UITableView alloc] initWithFrame:frame];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[table setDelegate:self];// app crash here 
 table.dataSource = self;// here also 
[table reloadData];
   [self.view addSubview:table];
 }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
  return 8;
 }
 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  {
  return @"test";
  }

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath;
   {
  return 190;
  }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
// Return the number of sections.
return 1;
  }
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
   static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = name;
return cell;
  }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
  }
 - (void)didReceiveMemoryWarning
  {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
   }
 @end
4

3 回答 3

1

我试过你的代码,它对我来说很完美,我想问题应该是视图控制器初始化,试试这个应用程序应该在应用程序启动结束时有一个根视图控制器,让我知道。

于 2013-04-23T11:09:13.583 回答
1

在这里,检查这是否发生在 App Delegate 中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[MainScreenViewController alloc] initWithNibName:@"MainScreenViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

并且在MainScreenViewController.m

@interface MainScreenViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UITableView * table;
@property (nonatomic, strong)NSString * name;

@end

@implementation MainScreenViewController
@synthesize table,name;

- (void)viewDidLoad
{
    [super viewDidLoad];

    name = [[NSString alloc]init];
    name  = @"testing of row";

    CGRect frame = self.view.frame;

    table = [[UITableView alloc] initWithFrame:frame];
    table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    [table setDelegate:self];
    [table setDataSource:self];

    [self.view addSubview:table];

    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"Test";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath;
{
    return 190;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    //    cell.textLabel.text = name;

    // Add UIScrollView for Horizontal scrolling.

    NSArray * colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor],[UIColor blueColor],[UIColor orangeColor], [UIColor blackColor],[UIColor purpleColor], nil];
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0,0.0,320.0,190.0)];
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * colors.count,scrollView.frame.size.height);

    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [scrollView addSubview:subview];
    }

    [cell.contentView addSubview:scrollView];

    return cell;
}
于 2013-04-23T11:20:24.870 回答
1

正如你所提到的

我已经添加了两者但仍然崩溃..我得到了这个 2013-04-23 12:37:28.361 AMM[2231:c07] 应用程序窗口预计在应用程序启动结束时有一个根视图控制器 2013-04-23 12:37:32.533 AMM[2231:c07] * -[MainScreenViewController respondsToSelector:]:消息发送到已释放实例 0x71edb70

在应用程序委托文件中,您是否将主视图控制器添加到窗口的根视图控制器?您也可以尝试使用一些硬编码文本而不是为单元格文本分配名称。

于 2013-04-23T10:46:50.007 回答