我想显示表格视图,其中每个单元格都包含嵌套的UIview,但我遇到了一些问题。
1)当我尝试滚动表视图时,我的应用程序崩溃。
通过将根视图而不是子视图添加到app delegate
来解决现在问题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;
}
**表格崩溃问题已解决**
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
答案 0 :(得分:1)
如你所说
我添加了两个但仍然崩溃..我得到了这个2013-04-23 12:37:28.361 AMM [2231:c07]应用程序窗口应该在应用程序启动结束时有一个根视图控制器2013- 04-23 12:37:32.533 AMM [2231:c07] * - [MainScreenViewController respondsToSelector:]:发送到解除分配的实例0x71edb70的消息
在应用程序委托文件中,您是否已将主视图控制器添加到窗口的rootviewcontroller中?您也可以尝试使用一些硬编码文本,而不是将名称分配给单元格文本。
答案 1 :(得分:1)
我尝试了你的代码并且它对我来说非常合适,我想问题应该是视图控制器初始化,试试这个Applications are expected to have a root view controller at the end of application launch并让我知道。
答案 2 :(得分: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;
}