Search code examples
iphoneiosios6

UITableview Crash when scroll up or down and how to set horizontal scroll view to every cell


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

Solution

  • 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.