Error with scrolling custom table cells in a tableview - ios

Hi I'm a newbie to iOS development I'm facing a problem regarding scrolling of custom cells in a tableview. My table view has around 7 cells but once I scroll the view after running I will be getting only first 5 cells like the below but even tough the rest of the cells are being displayed we can't completely scroll to have a look at those cell.
Here's my viewController.m code
#import "ViewController.h"
#import "MobileTableCell.h"
#interface ViewController ()
{
NSArray *tableData;
NSArray *thumbnails;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableData = [NSArray arrayWithObjects:#"Iphone 5s",#"Google Nexus 5", #"Samsung Galaxy S4",#"HTC one", #"LG G2", #"Moto X", #"Micromax Turbo", nil];
thumbnails = [NSArray arrayWithObjects:#"iphone5.jpg",#"nexus5.png",#"galaxys4.jpg",#"htcone.jpg",#"lgg2.jpg",#"motox.png",#"micromaxcanvasturbo2.jpg", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MyMobileTableCell";
MobileTableCell *cell = (MobileTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
//NSLog(#"%#",cell);
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MobileTableCell" owner:self options:Nil];
cell = [nib objectAtIndex:0];
}
// cell.layer.shouldRasterize = YES;
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
#end
Please lemme know where I have gone wrong. Thanks in advance.

You are using a UIViewController to render the UITable because you have: #interface ViewController : UIViewController <UItableViewDelegate , UITableViewDataSource>. Better to directly use the UITableViewController provided #interface ViewController : UITableViewController. If you have a valid reason for using a UIView then you need to set the delegate and dataSource to self:
tableView.delegate = self;
tableView.dataSource = self;
You also need to make sure there isn't anything else that is scrollable on the UIView that makes the table view scroll out of the page like a UIScrollView or something like that. You can use auto layout to help keep your stuff in view.
Last point, your issue might be the result of a UINavigationController covering parts of the table. If you are using a UIView you need to add content margin to the UITable to make sure the navigationController does not hide rows.

Related

UICollectionView Appearing Blank

I am trying to use a UICollectionView but I am having a lot of issues. I have a UIViewController with a UICollectionView inside. I have setup all the delegates correctly along with the UILabel in the CVcell. The view appears and is white as defined in the code. The NSLog sends out all the cells and the text that should be there but in the actual UICollectionView it is blank and nothing appears apart from the scroll view. I have tried everything but cannot seem to get this working correctly. The cell is 187 x 187 taking up half the screen size with a label in the middle. I will post the code below.
Thanks
#import "StudentControllerMain.h"
#import "CVcell.h"
#interface StudentControllerMain ()
#property (nonatomic, strong) IBOutlet UICollectionView *collectionView;
#property (nonatomic, strong) NSArray *dataArray;
#end
#implementation StudentControllerMain
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *firstSection = [[NSMutableArray alloc] init];
for (int i=0; i<5; i++) {
[firstSection addObject:[NSString stringWithFormat:#"Cell %d", i]];
}
self.dataArray = [[NSArray alloc] initWithObjects:firstSection, nil];
[self.collectionView registerClass:[CVcell class] forCellWithReuseIdentifier:#"CVcell"];
// Configure layout
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
self.collectionView = nil;
self.dataArray = nil;
// Dispose of any resources that can be recreated.
}
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [self.dataArray count];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSMutableArray *sectionArray = [self.dataArray objectAtIndex:section];
return [sectionArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell identifier
static NSString *cellIdentifier = #"CVcell";
CVcell *cell = (CVcell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *cellData = [data objectAtIndex:indexPath.row];
NSLog(cellData);
[cell.title setText:cellData];
[cell.title setTextColor:[UIColor blackColor]];
return cell;
}
#end
remove the register class code;
setting the "identifier" attribute (in attribute inspector) of collection view cell in storyboard;
If you're sure cellForItemAtIndexPath is being called then the issue is the frame of all the views as they are laid out. Run the app and go to that page. Go to Debug -> View debugging -> Capture view hierarchy . This will show you all the views and their sizes. Do you see the table itself? Is it the correct height? Can you see each cell? Can you see their subviews? You can right click each one to print out their dimensions.

Getting to a black screen while setting up grouped table view

I have a nib file that I have put inside of this nib a UITableView (grouped), sets the delegate and datasource of the table view to the file's owner, and created an outlet reference to this table view in the table view controller (files owner).
in the TargetsTableViewController i'v just set it up to see something, without some collection, just manually said I want 1 section and 5 rows:
#import "TargetsTableViewController.h"
#interface TargetsTableViewController ()
#end
#implementation TargetsTableViewController
- (id)init {
self = [super initWithNibName:#"TargetsTableViewController" bundle:nil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc]initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(close)];
self.navigationItem.rightBarButtonItem = closeButton;
}
-(void)close {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TargetsTableViewCell" forIndexPath:indexPath];
if (!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TargetsTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
// Configure the cell...
return cell;
}
and I have also a basic nib for a cell.
but when I run it I get this black screen:
does anyone know why is this?
thanks
try set tableview's frame or use some auto layout. You can use tableviewcell without register it. You may seeing the backgroundColor of UIWindow, and you got nothing on view to see, or in another word, a tableview with frame of CGRectZero.

How can I return a TableView from a instance method?

I am trying to add a TableView as a subview in my RootViewController.The TableView will come from another ViewController(TableViewGeneratorController) instance method.
So,What is the best way to do this?
I have created a TableViewGeneratorController it works fine as a standalone app. Then from my RootViewController I have created one instance of the TableViewGeneratorController and trying to call the instance method prepareField,which will return the TableView. I got the TableView but
numberOfRowsInSection and cellForRowAtIndexPath is not getting called.
TableViewGeneratorController.h
#import <UIKit/UIKit.h>
#interface TableViewGeneratorController:UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tableView;
}
#property(strong,nonatomic)UITableView *generatedTbleView;
- (UITableView *)prepareField;
#end
TableViewGeneratorController.m
#import "TableViewGeneratorController.h"
#import "RootViewController.h"
#interface TableViewGeneratorController (){
}
#end
#implementation TableViewGeneratorController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableView *)prepareField
{
tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 80, 300, 500)];
tableView.dataSource = self;
tableView.delegate = self;
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
[tableView reloadData];
return tableView;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = #"Yes";
return cell;
}
#end
RootViewController.m
Here I am trying to add the TableView as a subview.
TableViewGeneratorController *tableViewGeneratorController = [[TableViewGeneratorController alloc]initWithNibName:#"TableViewGeneratorController" bundle:nil];
UITableView *tv = [tableViewGeneratorController prepareField];
[self.view addSubview: tv];
What is the problem going on?
Thanks
What you are doing is not really the proper way.
The immediate issue is that the TableViewGeneratorController instance that you create goes out of scope and is deallocated. This leaves the table view with not existing data source or delegate. A simple workaround is to assign the TableViewGeneratorController instance to an instance variable instead of a local variable.
But the proper solution is to embed the TableViewGeneratorController as a child controller of the root view controller.
Change TableViewGeneratorController to be a UITableViewController and get rid of the prepareField method.
Then when you create the TableViewGeneratorController, you add it as a child controller. See the docs for UIViewController for details.

UITableView Cell Redirect

I am creating an UITableView and has a question on how to redirect the user to a new view when the person clicks on the cell. It would helpful if could provide some code and possible an explanation. Thank you :)
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
IBOutlet UIButton *Startbutton;
}
#property (strong,nonatomic) NSArray *array;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Startbutton.layer.cornerRadius = 5; // this value vary as per your desire
Startbutton.clipsToBounds = YES;
//Status Bar
[self setNeedsStatusBarAppearanceUpdate];
//Array
self.array = [[ NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
//Array Main Code
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#end
There is a pretty basic way of doing this without having to write code at all, but I am not sure if it is exactly what you want, sorry if it doesn't help.
Drag in a UITableViewController, in the Attributes inspector, select Static Cells from the 'Content' drop down box. Then add how ever many cells you like, click on the cell, then under the attributes inspector change the 'Style' to whatever you like and then change the content of the cell. Then all you have to do to link that cell to a new view is; right click the cell and drag your cursor to the destination view, then Select Modal (or Push if you are in a navigation controller).
That way when you run the app and click on that cell you should be switched to the new view.
No coding is required at all.
Hoped that helped in someway.
Cheers

UITableView crash on scroll or tap - issue with ARC?

I am trying to create a simple UItableview app in a project utilizing ARC. The table renders just fine but if I try to scroll or tap a cell the app crashes.
Looking at the NSZombies (is that the proper way to say that?) I get the message "-[PlacesViewController respondsToSelector:]: message sent to deallocated instance 0x7c29240"
I believe this has something to do with ARC as I have successfully implemented UItableviews in the past but this is my first project using ARC. I know I must be missing something very simple.
PlacesTableViewController.h
#interface PlacesViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
#property (nonatomic, strong) UITableView *myTableView;
#end
PlacesTableViewController.m
#import "PlacesTableViewController.h"
#implementation PlacesViewController
#synthesize myTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
[self.view addSubview:self.myTableView];
}
#pragma mark - UIViewTable DataSource methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *result = nil;
static NSString *CellIdentifier = #"MyTableViewCellId";
result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(result == nil)
{
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
result.textLabel.text = [NSString stringWithFormat:#"Cell %ld",(long)indexPath.row];
return result;
}
#end
There is nothing obviously wrong with the code you posted. The problem is with the code that creates and holds onto PlacesViewController. You are probably creating it but not storing it anywhere permanent. Your PlacesViewController needs to be saved into an ivar or put in a view container that will manage it for you (UINavigationController, UITabController or similar)

Resources