I want to create a default static table in my sub-class of UITableViewCintroller, but data doesn't appear. I don't know where is the problem, I think that cellForRowAtIndexPath method not called.
Here is my code:
#import <UIKit/UIKit.h>
#interface S5WebTable : UITableViewController <UITableViewDelegate, UITableViewDataSource>
#end
#import "S5WebTable.h"
#interface S5WebTable ()
#property(assign,nonatomic) BOOL isLoaded;
#end
#implementation S5WebTable
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView sizeToFit];
}
-(void)viewWillAppear:(BOOL)animated{
}
- (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{
return 7;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text =#":)";
return cell;
}
I think you have to use a distinct identifier for each cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"s%i-r%i", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
//you can customize your cell here because it will be used just for one row.
}
return cell;
}
First of all you have to give identifier to tableview cell then it will displays.
e.g.
NSString *cellIdentifier = [NSString stringWithFormat:#"%d_cell", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]];
}
return cell;
}
1.Drag TableView Controller in StoryBoard
2.Sele ct the TableView and in Right Inspector change number of Prototyes Cell(number of cells) and then Dynamic Prototypes to Static Cell
3.Customise the Cell according to your requirement
Tat's it Static TableView is created.Don't override any of tableview delegate or datasource methods
Below i added the screenshot of Right Inspector
Related
I have two viewControllers, ViewController and TableViewController. the ViewController has a button, when pressed the TableViewController will start. the TableViewContoller contains 5 rows.
what I am trying to do is, when I press only the even rows I should go back to ViewController. To solve this problem, we can use unwindSegue, but I do not know how to have the segue respond to the even rows in the table.
please let me know how to make a row in the table linked to the segue
below is the code for TableViewController
code:
#import "TableViewController.h"
#interface TableViewController ()
#property NSArray *tableData;
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableData = [NSArray arrayWithObjects:#"Android",
#"iOS",
#"swift",
#"objective-c",
#"openCV",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
return [self.tableData count];
}
-(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 = [self.tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"images.jpg"];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSLog(#"%li", indexPath.row);
NSLog(#"%#", [self.tableData objectAtIndex:indexPath.row]);
}
#end
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
if(indexPath.row % 2 == 0) {
// Even row...
}
}
I have a tableview called AdminOrderViewController and it has customcell called StepperProgressCell.
This customcell has a custom UIView called AYStepperView. There is a button in this UIView and I implemented a delegate on it, whenever it gets clicked and I clicked method is getting called on AdminOrderViewController.
However, I could not able to figure out how to pass clicked header cell.section to AYStepperView ??
AdminOrderViewController.m
#interface AdminOrderViewController : UIViewController <AYStepperViewDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.stepperView.delegate= self;
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
AdminHeaderFooterView *sectionHeaderView = [self.adminTableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
if (sectionHeaderView == nil)
{
sectionHeaderView = [[AdminHeaderFooterView alloc] initWithReuseIdentifier:SectionHeaderViewIdentifier];
}
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectHeaderAction:)];
[sectionHeaderView addGestureRecognizer:tapGesture];
return sectionHeaderView;
}
-(void) selectHeaderAction :(UITapGestureRecognizer*) gestureRecognizer
{
AdminHeaderFooterView* cell = (AdminHeaderFooterView*)gestureRecognizer.view;
[self toggleSection:cell withSection: cell.section];
// how to pass clicked section to AYStepperView??
}
-(void)clicked :(NSUInteger) currentSection
{
NSLog(#"Stepper clicked %lu", currentSection);
}
StepperProgressTableViewCell.m
#implementation StepperProgressTableViewCell
#synthesize stepperView;
- (void)awakeFromNib {
[super awakeFromNib];
[self setUpViews];
}
- (void)setUpViews {
self.stepperView = [[AYStepperView alloc]initWithFrame:CGRectMake(0, 0 , [[UIScreen mainScreen] bounds].size.width, kFormStepperViewHeight) titles:#[#"Processing",#"Ready",#"Delivered", nil)]];
[self addSubview:self.stepperView];
}
AYStepperView.h
#property (nonatomic) NSUInteger currentSection;
AYStepperView.m
#protocol AYStepperViewDelegate <NSObject>
#required
- (void)clicked :(NSUInteger) currentSection;
#end
- (void)buttonPressed:(UIButton *)sender {
[stepperDelegate clicked : currentSection];
}
The cell should not need to know which row or section it is in; Your table view controller can find this easily, given a reference to the cell.
Your view controller should not set itself as the delegate of the stepper view. It should be a delegate of the cell. The cell should be the delegate of the stepper view. This is a bit more complicated but it maintains better separation of concerns and makes the whole thing cleaner.
AYStepperView.h
#protocol AYStepperViewDelegate <NSObject>
#required
- (void)clicked;
#end
AYStepperView.m
- (void)buttonPressed:(UIButton *)sender {
[stepperDelegate clicked];
}
StepperProgressTableViewCell.h
#protocol StepperProgressTableViewCellDelegate <NSObject>
#required
- stepperChanged: (StepperProgressTableViewCell) cell;
StepperProgressTableViewCell.m
-(void)awakeFromNib {
self.stepperView.delegate= self;
}
- (void)clicked {
[self.delegate stepperChanged: self];
}
AdminOrderViewController.m
#interface AdminOrderViewController : UIViewController <StepperProgressTableViewCellDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.delegate= self;
return cell;
}
-(void)stepperChanged:(StepperProgressTableViewCell)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Now do something with indexPath.section
}
in cellForRowAtIndexPath set section to stepper view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.stepperView.delegate= self;
cell.stepperView.currentSection = indexPath.section;//set section value
return cell;
}
In XCode 5, when you add a UITableViewController with more than 1 prototype cell which all contain labels etc., the content is displayed because the UITableViewController automatically sets the data source and delegate of the table view and sends a reloadData message to it.
iOS Developer Library - Basics of Table View Creation
But when you drag a UITableView to a UIViewController I believe you have to create instance methods similar to the default ones below in order for the content to be reloaded and made visible.
My question is how can I target a UITableView somewhere within a UIViewController and set its data source and delegate and have it reloaded and its content displayed properly?
Here is the default code for the UITableViewController:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
Here is my code that works with UITableViewController but not in UIViewController for the UITableView that it contains:
...
#property (nonatomic, strong) NSArray *menuItems;
...
- (void) viewDidLoad
{
[super viewDidLoad];
self.menuItems = #[#"rowOne", #"rowTwo", #"rowThree", #"rowFour"];
}
...
- (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;
}
IN your code where u have created your cell if it not exists? And also make cell identifier as static.
You should create cell if it not exists in cellForRowAtINdexPath like as following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
//Create your cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Configure your cell
return cell;
}
You can set data source and delegate in a few ways. If you add table view to view controller in storyboard just control drag from table view to view controller and select delegate and do it again and select data source.
You can also create outlet and in your view controller in viewDidLoad do it by:
self.tableView.dataSource = self;
self.tableView.delegate = self;
The other way is create table view in code and add it to your view controller view. You can do it in viewDidLoad like that:
tableView = [[UITableView alloc] initWithFrame:CGRectMake(TABLE_VIEW_FRAME) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
If you want to reload the data just call:
[self.tableView reloadData];
Hope this help.
Make sure you implement UITableViewDelegate and UITableViewDataSource protocols and set these in the initWithNibName or initWithCoder initializers.
This is how your .h file should look like:
#interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Here is what you need to do in the .m file:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
if you are using storyboards, please use initWithCode as given below:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
You are only dequeuing the cell but you are not creating one if dequeueReusableCellWithIdentifier returns nil. You should implement in following way.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// here you can configure the cell
return cell
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've a simple View that is the third level of a UINavigationController, it show an empty table from a nib, here is the code of the .m file:
#import "ThirdLevel.h"
#implementation ThirdLevel
#synthesize lista, categoria;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"Categoria: %#", categoria);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return NO;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//newtableView.separatorColor = [UIColor clearColor];
static NSString *CellIdentifier = "Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
}
- (void)buttonPressed:(id)sender {
NSLog(#"premuto");
}
- (void)dealloc {
[super dealloc];
}
#end
When i run it, the device crash and the debugger say that there is a EXC_BAD_ACCESS at this line:
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
i've the same code in the second level of the UINavigationController and it work fine, realy don't understand what's wrong.
Thanks for any help :)
static NSString *CellIdentifier =#"Cell";
not
static NSString *CellIdentifier = "Cell";
more over
return cell;//not found on ur code
May be you should return cell in this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//newtableView.separatorColor = [UIColor clearColor];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
Updated:
And as mentioned #AppleVijay add # when initializing CellIdentifier