1

I want to display table view in which every cell contain nested UIview but i am facing some problems.

1) My app crash when i try to scroll table view.

Solved by add root view instead of subview into app delegate

Now problem number 2

2) I want to add horizontal scroll view inside table cell.so i can display 3 subview in single cell and i can scroll horizontally with in cell..how can i do that. I want to do this..

enter image description here

To achive this i have code this..

 - (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;
   }

and call this view from table cell

- (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;

}

by adding wg i get one train boggi, wagon view.. i want 2 wagon view and engine at last or first.

** Table crashing problem is solve**

1) to solve crashing problem i search on stackoverflow and i found solution like adding to delegates or change to retain or strong bla bla.. but non of these work for me. Here is my code. and one more thing i am not use XIB , nib or storyboard..

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

.m file

@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
Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77
  • Please try :@interface MainScreenViewController : UIViewController and @property (strong, nonatomic) IBOutlet UITableView *table; – Sovannarith Apr 23 '13 at 10:34
  • @sovannarith i have added both but still it crash..i got this 2013-04-23 12:37:28.361 AMM[2231:c07] Application windows are expected to have a root view controller at the end of application launch 2013-04-23 12:37:32.533 AMM[2231:c07] *** -[MainScreenViewController respondsToSelector:]: message sent to deallocated instance 0x71edb70 – Swap-IOS-Android Apr 23 '13 at 10:38
  • OH, please try with xib – Sovannarith Apr 23 '13 at 10:42
  • When your App load, your data is present but when u scroll it, crash ? – Sovannarith Apr 23 '13 at 10:44
  • 1
    @sovannarith thanks for help..but problem is in app delegate ;; i am using subview instead or root view.. – Swap-IOS-Android Apr 23 '13 at 11:33
  • Create 2 set of scrollview, one for odd Indexpath (with Engine at front) and another for Even Indexpath (with Engine at last). And set appropriate scrollview based on indexpath in cellForRowAtIndexpath:. – Vedchi Apr 23 '13 at 12:26

3 Answers3

1

As you have mentioned

i have added both but still it crash..i got this 2013-04-23 12:37:28.361 AMM[2231:c07] Application windows are expected to have a root view controller at the end of application launch 2013-04-23 12:37:32.533 AMM[2231:c07] * -[MainScreenViewController respondsToSelector:]: message sent to deallocated instance 0x71edb70

In the application delegate file have you added the mainview controller to window's rootviewcontroller? Also could you try with some hardcoded text instead of assigning name to cell text.

user2071152
  • 1,161
  • 1
  • 11
  • 25
  • Yes, if this is the first view which has to be shown on selecting this application. – user2071152 Apr 23 '13 at 10:57
  • I display splashscreenviewcontroller and than i display Mainscreenviewcontroller..so i though i dont need it.. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. splashScreenViewController *controller = [[splashScreenViewController alloc] initWithNibName:nil bundle:nil]; [self.window addSubview:controller.view]; [self.window makeKeyAndVisible]; return YES; } – Swap-IOS-Android Apr 23 '13 at 11:00
  • ok, then no need to add MainScreenViewController as rootWindow. Could you check by assigning a hardcoded text to cell text. – user2071152 Apr 23 '13 at 11:03
  • i tried hardcoded text instead of assigning name variable..but still crash – Swap-IOS-Android Apr 23 '13 at 11:05
  • Could you try debug step by step and check where the crash is observed. – user2071152 Apr 23 '13 at 11:09
  • Solved by [self.window setRootViewController:someController]; in app delegate – Swap-IOS-Android Apr 23 '13 at 11:12
  • 1
    Good. This was my initial answer. – user2071152 Apr 23 '13 at 11:14
1

I tried your code and it is working perfectly for me , I guess problem should be with view controller initialisation , try this Applications are expected to have a root view controller at the end of application launch and let me know.

Community
  • 1
  • 1
karthik
  • 1,271
  • 1
  • 14
  • 27
1

Here, Check this is happening in 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;
}

and in 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;
}
Vedchi
  • 1,200
  • 6
  • 14
  • Thanks for sample..i want to display train and on that train i am displaying imageview to display video poster and at the end of train Engine .. and train engine on 1st row is on right hand side and in 2nd row left hand side and vice versa..how can i provide 2 layout for individual row in tableview – Swap-IOS-Android Apr 23 '13 at 11:31
  • please take a look at main question i have added image in it – Swap-IOS-Android Apr 23 '13 at 11:56