I just followed a few steps in a tutorial to create a simple TableView with 50 rows but i get "Signal SIGABRT" :/
I connected the TableView in Storyboard with the TableViewController-Class i created.
Here's my simple code:
#import "TableViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (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 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:#"Row %i",indexPath.row];
return cell;
}
Welcome to Stack Overflow! The standard methods for setting up UITableViewCells changed slightly a little while ago. This means that the template code Xcode provides for tableViews uses the -tableView: dequeueReusableCellWithIdentifier: forIndexPath: method, while older tutorials and books (most of them) use tableView: dequeueReusableCellWithIdentifier:
If you want to do it the new way ( -tableView: dequeueReusableCellWithIdentifier: forIndexPath) you need to either add
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"]; in viewDidLoad, (or set the prototype cell reuse id in the storyboard/nib, and set the cell type appropriately - basic should do for a normal cell).
The old way (tableView: dequeueReusableCellWithIdentifier:) is normally followed by an if statement something like:
if(cell == nil)
{
cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
(where the \\Configure the cell. . . comment is)
While this is really straightforward stuff, in fairness most tutorials do teach the old way, which I imagine can be confusing for a beginner if you don't notice the small difference between the two -tableView:dequeueReusableCell methods. A tutorial showing the new way is here
Related
I have a table view controller in which the cells don't show up:
I have added UiViewDataSource and UITableViewDelegate as shown below:
myViewController:UITableViewController<UITableViewDataSource,UITableViewDelegate>
In the storyboard as well as in the code I have set
self.tableView.delegate =self;
self.tableview.datasource = self;
My numberOfRowsInSection returns the value 30 (verified with logs) and numberOfSectionsInTableView returns 1 (verified with logs)
I have also printed the tableView header and footer view frame,height and width and they all have the value 0.0000
NSLog(#"The header frame size is%f" ,self.tableView.tableHeaderView.frame.size.height);
NSLog(#"The header frame size is%f" ,self.tableView.tableHeaderView.frame.size.width);
NSLog(#"The header frame size is%f" ,self.tableView.tableFooterView.frame.size.height);
NSLog(#"The header frame size is%f" ,self.tableView.tableFooterView.frame.size.width);
This is the firstController of a navigation controller.
The numberOfSectionsInTableView method is called first, then the numberOfRowsInSection and then the heightForRowAtIndexPath is called.
The cellForRowAtIndexPath method contains:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureCustomCell:[self.dataSource objectAtIndex:indexPath.row]];
return cell;
}
configureCustomCell configures the different properties in the cell.
I tried all the solutions given in similar questions but none of them seem to be working. What am I missing?
if you have configured UITableViewDataSource and UITableViewDelegate protocols inside your ViewController.h file, then there is no need to use self.tableView.delegate =self;
self.tableview.datasource = self;
if you have configured UITableView inside your storyboard.
you need to configure your CustomTableViewCell with your tableView's cell property.
step - 1
select your tableView Cell and inside identity inspector define your CustomTableViewCell class.
step - 2
now provide cell identifier. (i have used "customCell")
import your CustomCell class inside your ViewController.m file. Just created demo app as per your reference. Hope it solves your query.
#import "ViewController.h"
#import "CustomTableViewCell.h" //CustomTableViewCell class
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 30; //used staticCells 30
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"customCell"; //as defined in cell identifier (as per step -2)
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// configure your custom cell
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I'm pretty new to iOS dev so please bear with me.
I created a simple app that had a TabBarController with 2 items. The first item was a TableViewController with some cells and the 2nd item was a simple UIView with a picture and some text. Everything worked just fine in the simulator. This was all done programmatically (did not use storyboard)
I wanted to practice using the storyboard so I decided to try and replicate the original program I made using a storyboard. The problem arises when created a TableViewController. It's asking me to specify a "reuse identifier" , at first I tried naming them all "cell" but it said I couldn't use the same identifier, so then I set the first one to "cell1" and the next to "cell2" and so on.. I didn't have to do this when I wrote the app with just code, so why now?
My coded TableViewController looks like:
#implementation FeedTableViewController
- (id)init //constructor
{
self = [super init];
if (self)
{
self.title = #"Feed";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Title"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Title"];
}
cell.textLabel.text = [NSString stringWithFormat:#"%li", indexPath.row];
return cell;
}
in the last method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I just named them all "title" and it ran fine.
Thanks in advance for the help
Trying to add a section with 6 cells in my table. I just have some basic code and it is failing to compile giving me "'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'". I believe it has something to do with not setting a datasource, but I am not sure. Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
germanMakes = #[#"Mercedes-Benz", #"BMW", #"Porsche",
#"Opel", #"Volkswagen", #"Audi"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return germanMakes.count;
}
You must impement the considered method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = germanMakes[indexPath.row];
return cell;
}
You have not implemented the datasource method which is "cellForRowAtIndexPath" method, make sure you also connect the datasource of table view to your class in Interface builder
I'm using this tutorial on how to create a pop out menu.
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
I got it working when going through the tutorial, now i'm trying to implement it into my app.
I'm at the stage where i can press the menu button and the popout menu appears. The problem is, it doesn't populate itself with my table view cells.
I set the identifiers for each table view cell and then in the code reference them to full an array.
I know when woking through the tutorial, if I misspelled one of the identifiers when defining what's in the array, the program would crash. In my app, that's not the case. Hopefully that can help pin down the problem. It doesn't even change the colours which is the first part of the code.
Here's the code.
#import "SidebarViewController.h"
#import "SWRevealViewController.h"
#interface SidebarViewController ()
#property (nonatomic, strong) NSArray *menuItems;
#end
#implementation SidebarViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
_menuItems = #[#"markup",#"tax"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Any help would be great.
Thanks
All you're doing is creating an array with two string literals. You must set the textlabel's text property in the following method to display the strings in table view cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [_menuItems objectAtIndexPath:indexPath.row];
return cell;
}
OMG I feel like such a noob. It turns out the ViewControler didn't have a target, hence why it wasn't responding.
I'm so sorry for wasting your time, I really feel bad now.
I have a programmatically implemented tableView, in the grouped style.
All I am getting is the gray pinstripes when it should be populated. So it is loading, but not ... something...
What more is necessary?
If no more, then where else ought I look?
Also, how can I make the background color of the table the same as the cell's white color?
- (void)loadView {
[super loadView];
UITableView *view = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
self.view = view;
[view reloadData];
}
Is viewDidLoad necessary?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
Thank you,
Morkrom
You have to provide your tableView with data.
For staters you'll need to define a dataSource. Its common to just use your viewController as the dataSource.
// in the .h find something similar and add <UITableViewDataSource>
#interface ViewController : UIViewController <UITableViewDataSource>
then when you make the tableView.
view.datasource = self;
Then you'll need to provide the data itself. Implement these methods:
#pragma mark - UITableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:#"A Cell"];
return cell;
}
Those methods will create 3 sections each with 3 rows. All the cells will just say A Cell. This is the basis for all tableViews. Just customize the data :)
You need to set dataSource and delegate properties for your table view so it will be able to pull data from them:
UITableView *view = ...
view.dataSource = self;
view.delegate = self;
have protocol in .h file and attach delegate and source with file owner