group static tableView in storyboard does not appear - objective-c

In storyboard I create static Group table view contain 4 rows when I add my tableView classes to the storyboard it dosen't show my group cell
would you please help me (I'm new to iOS please show me with code)
Thanks in Advance!
here is the code for tableView Class
#import "CheckedInOut.h"
#interface CheckedInOut ()
#end
#implementation CheckedInOut
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *menuButton= [[UIBarButtonItem alloc] initWithTitle:#"Menu" style:UIBarButtonItemStyleDone
target:self action:#selector(menu:)];
UIBarButtonItem *yearButton= [[UIBarButtonItem alloc] initWithTitle:#"Year" style:UIBarButtonItemStyleDone
target:self action:#selector(year:)];
UIBarButtonItem *weekButton= [[UIBarButtonItem alloc] initWithTitle:#"Week" style:UIBarButtonItemStyleDone
target:self action:#selector(week:)];
UIBarButtonItem *reportButton= [[UIBarButtonItem alloc] initWithTitle:#"Report" style:UIBarButtonItemStyleDone
target:self action:#selector(report:)];
NSArray *buttons = [NSArray arrayWithObjects:menuButton,yearButton,weekButton,reportButton,nil];
self.navigationItem.rightBarButtonItems = buttons;
}
- (void)viewDidUnload
{
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
I don't know what should I write here for return
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
}
#end

you should remove all these 3 if you are using static table in storyboard
- (NSInteger)tableView:
- (UITableViewCell *)tableView:
- (void)tableView:(UITableView *)

Related

TableViewCell push to another ViewController very laggy

I embedded the navigation controller to the UITableViewController, and for every cell I need click the cell to push to another viewcontroller. The problem is when I enable the animation, it would be very laggy clicking the cell. I don't know why.
The code is following as:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = #"test";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SliderViewController *vc = [[SliderViewController alloc] init];
[self.navigationController pushViewController:vc animated:NO];
}
I think it is quite normal so I don't know what's the problem.

Unable to reloaddata(UITableView)

I'm creating a UITableView in an UIView controller
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 470, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
NSLog(#"tableView is '%#'",self.tableView);
}
the NSLog result :"tableView is '< UITableView: 0x7c60c00; frame = (0 0; 470 1004); clipsToBounds = YES; gestureRecognizers = ; layer = ; contentOffset: {0, 0}>'"
the refreshTableView method will be called, when the itemArray is modified by other controller
**SideBarViewController *sideBarViewController = [[SideBarViewController alloc]init];
[sideBarViewController refreshTableView];**
-(void)refreshTableView {
[self createItemArray];
NSLog(#"reload itemArray->%#",self.itemArray);
NSLog(#"tableView is '%#'",self.tableView);
[self.tableView reloadData];
}
somehow, the NSLog result become:"tableView is '(null)'". and the tableView won't be reloaded.
why? please help. thanks.
Update:
the NSLog of itemArray was fine:
before change
reload itemArray->(
"test.md"
)
after change
reload itemArray->(
"test.md",
"test2.md"
)
Update2:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.itemArray count];
}
- (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 = [self.itemArray objectAtIndex:indexPath.row];
return cell;
}
update 3:
.h
#interface SideBarViewController : UIViewController<IIViewDeckControllerDelegate,UITableViewDataSource,UITableViewDelegate>
.m
#interface SideBarViewController ()
#property (nonatomic,strong) UITableView *tableView;
#end
#implementation SideBarViewController
#synthesize tableView = _tableView;
update 4:
the
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
method is working fine, after deleted rows the tableview was reloaded.
Share some more code so we can find the exact issue.
can you check the actual name of your tableView? share some code of the table you have declared in .h file.
Check array, data is available before reload or update and table view methods are working fine.
Check you define tableView delegates in .h file or not? And implement delegate methods of tableView in your class properly..
#interface MyViewController:UIViewController
And one more thing check you have properly implemented your data source and delagate methods.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
If you have done this thing correctly then check your array before load it contains data or not. May be this will help you.
You can try to use Notification
add Observer into you viewDidLoad in Sidebarvc
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(refreshTableView:)
name:#"dataChanged"
object:nil];
create a method to refresh
-(void)refreshTableView:(NSNotification *)notif{
}
on the other VC,just wait for the notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"dataChanged"
object:nil];
4.don't forget to clean it
add [[NSNotificationCenter defaultCenter] removeObserver:self]; to ViewDidUnload
Should work.
//
// ViewController.h
// tableData
//
// Created by Nirav Jain on 3/28/13.
// Copyright (c) 2013 SI. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
int rows;
}
#end
// tableData
//
// Created by Nirav Jain on 3/28/13.
// Copyright (c) 2013 SI. All rights reserved.
//
#import "ViewController.h"
//#implementation SideBarViewController
#interface ViewController ()
#property (nonatomic,strong) UITableView *tableView;
#end
#implementation ViewController
#synthesize tableView = _tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 470, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
NSLog(#"tableView is '%#'",self.tableView);
rows = 5;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(refreshTableView)];
}
-(void)refreshTableView {
rows = 12;
NSLog(#"tableView is '%#'",self.tableView);
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return rows;
}
- (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 = [ NSString stringWithFormat: #"%d", indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Why is my UITableView not animating all rows the same, when leaving editing mode?

This is similar to my previous question, but my previous question was actually somewhat answered, and this is kind of an extension of it.
I have a UITableView that I am putting into and out of editing mode. While in editing mode, I display an additional row with the insert icon. All other rows, from my data source, are displayed with a delete icon.
For some reason, the last row from my data source is not animating the same as all the other rows, when leaving editing mode. This looks like some obvious code issue to me, based on the behavior, but I can't seem to fix it. I made an animated GIF to show you what I'm talking about:
Does anybody know why this might be happening? It's always that last row from my data source. The custom insert row's icon animates out just fine. It's not a ton of code, but it's easiest to view it all at once, so I will link you to the exact code on Github:
https://github.com/ryancole/pound-client/blob/master/pound-client/controllers/ChannelListViewController.m#L100
It seems like the issue is not with the tableView or any code associated with that. It seems like it is causing because of the pull to refresh view. Unfortunately I don't have the code for that component. I tried your with out the pull to refresh and it is working perfectly. So please remove the pull to refresh and see what is happening.
#import <UIKit/UIKit.h>
#interface zViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITableView *table;
#end
#import "zViewController.h"
#interface zViewController ()
#property (nonatomic, retain) NSArray* data;
#end
#implementation zViewController{
BOOL _editing;
}
#synthesize data = _data;
#synthesize table = _table;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_editing = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButtonWasPressed:)];
[self loadRecords];
}
- (void)editButtonWasPressed:(id)sender {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(saveButtonWasPressed:)];
[self setEditing:YES animated:YES];
}
- (void)saveButtonWasPressed:(id)sender {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButtonWasPressed:)];
[self setEditing:NO animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
_editing = editing;
if (editing == YES) {
[self.table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.data.count -1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
[super setEditing:editing animated:animated];
[self.table setEditing:editing animated:animated];
if (editing == NO) {
[self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.data.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[self fetchChannels];
}
- (void)fetchChannels {
[self.table reloadData];
}
-(void) loadRecords{
self.data = #[#"Record 1", #"Record 2", #"Record 3"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return (indexPath.row == self.data.count)? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _editing? self.data.count + 1 : self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = self.data[indexPath.row];
return cell;
}
#end
The animation is correct. You are inserting/deleting last row in -setEditing:animated:, so there are two kinds of animations are going on.

Push the values to new ViewController when a cell is tapped

I have table view that populated with plist called "Htgg".
I am trying to push the value to a new view called "DetailViewController".
This is the code:
#import "Glist.h"
#import "DetailViewController.h"
#implementation Glist
#synthesize htgg,detailViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"ofek", #"ofek");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"איך להוציא גימלים?";
NSString *htggFile = [[NSBundle mainBundle]pathForResource:#"Htgg" ofType:#"plist"];
htgg = [[NSDictionary alloc] initWithContentsOfFile: htggFile];
}
- (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 [htgg count];
}
- (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.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
id keys = [htgg allKeys];
//Get all the keys in the first dictionary (the keys are: "good", "funny", "New - Item 3")
//This is an array, so you can do this: array[0] (in C#)
//Here we tell the tableview cell. to put in the text - [keys objectsAtIndex:indexPath.row]; if the row is 0, we will get: "good", if 1, we will get "funny"
cell.textLabel.text = [keys objectAtIndex:indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *showPickedSolution = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:showPickedSolution animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
The code works perfect but it doesn't push the value of the row( all the rows of plist are string type and above the there is 'Root').
I need your help to push the value to another view(DetailViewController).
make a property in your DetailViewController of the same type as of your data you want to push then use
DetailViewController *showPickedSolution = [[DetailViewController alloc]
initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
showPickedSolution.yourPropertName = [htgg objectAt:indexPath.Row];
[self.navigationController pushViewController:showPickedSolution animated:YES];
you can now access the passed dat in your DetailViewController in your property.
Please check the syntax.
I have done this in two different way depending on the scenario:
1) Delegate, when I fill some values on a form that I want to pass back to the original screen.
2) Storyboard Segue, when I want to send some value(s) to a second screen.
I usually put all the values I want in an array to send the object and then at arrival I extract what I need.
Instead of explaining each way, I can refer you to Getting Started section of the iOS Dev Site.
I hope it help!
To achieve this you can made an NSString property in class DetailViewController and set that property in didSelectRowAtIndexPath. Here is the code for your reference:
showPickedSolution.valueToBeSent = [htgg valueForKey:[[htgg allKeys] objectAtIndex:indexPath.row]];
You can use this valueToBeSent in your DetailViewController class.

Why is TableView not showing anything.

I have a class with a tableView. I get no build errors, but when I try to run the app, after I add a new item, nothing appears to happen. Here's the code:
In .h
#interface entriesViewController : UIViewController <UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *TableView;
-(IBAction)startEditing:(id)sender;
-(IBAction)newItem:(id)sender;
#end
In .m
#import "entriesViewController.h"
#import "LEItemStore.h"
#import "LEItem.h"
#interface entriesViewController ()
#end
#implementation entriesViewController
#synthesize TableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[TableView setDelegate:self];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[TableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)startEditing:(id)sender {
if ([TableView isEditing])
{
[TableView setEditing:NO animated:YES];
}
else
{
[TableView setEditing:YES animated:YES];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[LEItemStore sharedStore] allItems] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"UITableViewCell"];
}
LEItem *p = [[[LEItemStore sharedStore] allItems]objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[p description]];
return cell;
}
-(IBAction)newItem:(id)sender {
LEItem *newItem = [[LEItemStore sharedStore] createItem];
[TableView reloadData];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
LEItemStore *ps = [LEItemStore sharedStore];
NSArray *items = [ps allItems];
LEItem *p = [items objectAtIndex:[indexPath row]];
[ps removeItem:p];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[[LEItemStore sharedStore] moveItemAtIndex:[sourceIndexPath row] toIndex:[destinationIndexPath row]];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSArray *items = [[LEItemStore sharedStore] allItems];
//BNRItem *selectedItem = [items objectAtIndex:[indexPath row]];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self TableView]reloadData];
}
It would appear as if the TableView isn't reloading it's data. By the way, all connections have been made. Absolutely nothing appears. Just my Toolbar, the TableView does not change even after I added an item.
Ok so after clearing this in the comments, your problem is that you haven't set the data source (you can also do this with an array controller).
So you set the delegate and the data source:
[TableView setDelegate:self];
[TableView setdataSource: self];
Then you declare the class to also implement the UITableViewDataSource protocol.
And you provide the two fondamental methods (also optional methods are welcome):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
This is an example of a simple data source implementation that just has one label saying hello:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell=[[UITableViewCell alloc]init];
UILabel* label= cell.textLabel;
label.text= #"Hello";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
But now you know how to provide a more complex data source: if you have an array you can read the index path and then set the label using the objectAtIndex method.
I am not sure about this, but if you are using controller as data source then you need to specify interface for UITableViewDataSource.
Try changing:
#interface entriesViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
Set delegate and dataSource for tableView in viewDidLoad to controller instance e.g.
TableView.delegate = self;
TableView.dataSource = self;
You can also implement UITableViewDataSource interface on LEItemStore classs. If you do so, then you need to set dataSource property to instance of LEItemStore.
TableView.delegate = self;
TableView.dataSource = [LEItemStore sharedStore];