UITableView does not enter editing mode - objective-c

I've tried many times to create a table view and delete certain rows, I even asked this question here before and implemented what they advised but yet failed to even get my table
ViewController.h
#interface XYZViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (strong, nonatomic) UITableView *myTable;
#property (strong, nonatomic) NSMutableArray *names;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *editButton;
- (IBAction)editMyTable:(id)sender;
#end
ViewController.m
#implementation XYZViewController
#synthesize names, myTable, editButton;
- (void)viewDidLoad
{
[super viewDidLoad];
//input values into the mutable array
names = [[NSMutableArray alloc]initWithObjects:
#"Bob",
#"Chris",
#"Tom"
, nil];
editButton = self.editButtonItem;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Friends";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create an identifier
static NSString *identifier;
//create the cell with the identifier
UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:identifier];
//check if cell is nil
if (cell == nil) {
//assign cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//assign the names of the array to each cell
cell.textLabel.text = [names objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)editMyTable:(id)sender
{
[editButton setTitle:#"Done"];
[myTable setEditing:YES animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[myTable setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove from array
[names removeObjectAtIndex:indexPath.row];
//remove from tableView
[myTable deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Image: http://postimg.org/image/g9wqwuo6v/
Any help is much appreciated! Thank you in advance! :)

It appears that your issue is that you have not wired up your myTable property. It is not an IBOutlet and nowhere in your code do you establish the connection. When you call [myTable setEditing:YES animated:YES]; it is sending it to a nil table. You can verify this by printing out myTable's value before calling the edit method: NSLog(#"%#", myTable);.
Also you should remove your overriding of setEditing:animated: since you are a UIViewController subclass and not a UITableView subclass. Just making your initial call in your IBAction should be enough.

Related

Custom Collectionview under each tableview rows

I am new in objective c. I want to create a layout design like android app store. Which means, i have a tableview and each row of tableview has a collectionview, and one more thing, i am using xcode7 and i want to create a design with storyboard. When i should create a that type of design each tableview row show collectionview but my problem is that each collectionview have save data. but i want to different data in every row. I go throw many tutorial but i can't understand. Can anyone assist me, how to create that type of design? Now i am create a tableview datasourse and delegate method in viewcontroller.m file and also use collection view's datasourse and delegate method into custom tableview cells.m class. please any one assist me
each collectionview have save data. but i want to different data in every row
because your collection view's datasourse and delegate method in tableViewCell is same.maybe it's not ok to determine dataSource and delegate implementation in tableViewCell ,you can delegate it out to your ViewController, and each tableViewCell's has different implementation.
I have try to implementation a demo , it can work ,but i think may have better way. this is my code
Cell.h
//Cell.h
#import <UIKit/UIKit.h>
#class MTTableViewCell;
#protocol MTTableViewCellDelegate <NSObject>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section InTableViewCell:(MTTableViewCell *)cell;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath InTableViewCell:(MTTableViewCell *)cell;
#end
#interface MTTableViewCell : UITableViewCell<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
#property (nonatomic, weak) id<MTTableViewCellDelegate> delegate;
#property (nonatomic, strong) UICollectionView *collectionView;
#property (nonatomic, assign) NSInteger index; // [tablview indexPathForCell:] not work before cell is render
#end
Cell.m
//Cell.m
#import "MTTableViewCell.h"
#interface MTTableViewCell()
#end
#implementation MTTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
}
return self;
}
#pragma mark -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (_delegate && [_delegate respondsToSelector:#selector( collectionView:numberOfItemsInSection:InTableViewCell:)]) {
return [_delegate collectionView:collectionView numberOfItemsInSection:section InTableViewCell:self];
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath {
if (_delegate && [_delegate respondsToSelector:#selector( collectionView:cellForItemAtIndexPath:InTableViewCell:)]) {
return [_delegate collectionView:collectionView cellForItemAtIndexPath:indexPath InTableViewCell:self];
}
return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(50, 50);
}
#end
ViewController.m
#import "ViewController.h"
#import "MTTableViewCell.h"
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate,MTTableViewCellDelegate>
#property (nonatomic, strong) UITableView *tableView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 72.0;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MTTableViewCell *cell = (MTTableViewCell*) [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell) {
cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.index = indexPath.row;
cell.delegate = self;
[cell.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"CCell"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:( NSIndexPath *)indexPath {
MTTableViewCell *mtCell = (MTTableViewCell *)cell;
if (!mtCell.collectionView.superview) {
[mtCell addSubview:mtCell.collectionView];
mtCell.collectionView.delegate = mtCell;
mtCell.collectionView.dataSource = mtCell;
}
}
#pragma mark - MTTableViewCellDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath InTableViewCell:(MTTableViewCell *)cell {
UICollectionViewCell *cCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CCell" forIndexPath:indexPath];
cCell.backgroundColor = [UIColor redColor];
return cCell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section InTableViewCell:(MTTableViewCell *)cell {
[_tableView indexPathForCell:cell];
return cell.index
;
}
#end
Hope this can help you :)

Trying to call a second table view with the items of de selected playlist in the first view

Maybe there's someone outthere who can help me fiure out my problem. I cant seem to display the playlistItems using cocoalibspotify. I have set up my playlistview and the first ableviewcontroller shows the playlist but when i try to call the items of the selected playlist I seem to get 0 numbersof row as my outputs show. the first view show how i pass the indexpath to the secondviewcontroller. the second script show my .h and .m files of the playlistitemsTavle view controller.
Overview.m - tableView with Playlists
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[playlistView deselectRowAtIndexPath:indexPath animated:NO];
playlistItemViewController *trailsController = [[playlistItemViewController alloc] initWithStyle:UITableViewStylePlain];
trailsController.currentPlaylist = [playlistArray objectAtIndex:indexPath.row];
//[[self navigationController] pushViewController:trailsController animated:YES];
[self getSongs];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showPlaylistItem"]) {
UITableViewCell *BasicCell = (UITableViewCell *) sender;
NSIndexPath *ip = [self.tableView indexPathForCell:BasicCell];
SPPlaylist *selectedPlaylist = [playlistArray objectAtIndex:ip.row];
playlistItemViewController *pivc = (playlistItemViewController *) segue.destinationViewController;
pivc.currentPlaylist = selectedPlaylist;
}
}
playlistitemsViewController.h -
#import <UIKit/UIKit.h>
#import "CocoaLibSpotify.h"
#import "Simple_PlayerAppDelegate.h"
#import "overViewController.h"
#interface playlistItemViewController : UITableViewController
{
UITableView *tableView;
}
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#property (strong, readwrite, nonatomic) SPPlaylist *currentPlaylist;
#end
playlistViewController.m - this should call the playlist items
#import "playlistItemViewController.h"
#interface playlistItemViewController ()
#end
#implementation playlistItemViewController {
}
#synthesize currentPlaylist;
#synthesize tableView;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(#"numberOfRowsInSection: %d",[[currentPlaylist items] count]);
return [[currentPlaylist items] count];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SubtitleCell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[[currentPlaylist items] objectAtIndex:indexPath.row] valueForKey:#"name"];
return cell;
if (indexPath.row == 0) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SubtitleCell"];
[[cell.backgroundView superview] bringSubviewToFront:cell.backgroundView];
cell.textLabel.text = #"";
}
else{
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"SubtitleCell"];
}
SPPlaylistItem * item = [[currentPlaylist items] objectAtIndex:[[currentPlaylist items]count] - indexPath.row];
cell.textLabel.text = [item.item name];
if (item.itemClass == [SPTrack class]) {
SPTrack * track = item.item;
cell.detailTextLabel.text = [track consolidatedArtists];
}
}
return cell;
}
#end
You need to wait for the playlist to load before the items are available.
Your playlist view controller needs to use SPAsyncLoading to load the playlist, something list this:
[SPAsyncLoading waitUntilLoaded:self.currentPlaylist timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedItems, NSArray *notloadedItems) {
[self.tableView reloadData];
}];
Playlist loading can take a while, so make sure you put up some "loading" UI. You'll also need to load the visible tracks in a similar manner before their names will be available.

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 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];

create section in Dynamic UITableView in objective-c

I want to create Dynamic UITableView that's contain days as a sections (sat,sun,mon,...)
would you please helping me!
Thanks in advance!
here is my code but i don't know what should I write for creating sections:
Day.h
#import <UIKit/UIKit.h>
#interface Day : UIViewController
#property (nonatomic, strong) NSMutableArray *monthTitle;
#end
Day.m
#synthesize monthTitle;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super init];
if (self) {
monthTitle = [[NSMutableArray alloc] init];
}
return self;
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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.monthTitle count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:#"Cell"];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.monthTitle removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib
name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
Make sure that you set up your UIViewController as the delegate and dataSource of the UITableView. First you need to implement the protocols for these two:
#interface Day : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSMutableArray *monthTitle;
#end
Second you need to assign the delegate and dataSource of the UITableView to the UIViewController.
Right click on the UITableView and drag the delegate and dataSource outlets to the UIViewController at icon at the bottom of the view:
Now your UITableView is set up to use your UIViewController as the delegate and dataSource. After that, set up the functions properly to fill the UITableView with data:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.monthTitle count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.monthTitle objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1; //Return the number of sections you want in each row
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Whatever You Put For Cell Identifier On Interface Builder";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Configure the cell however you wish
return cell;
}
Now, assuming you have self.monthTitle set up as an NSArray of NSString that holds the titles you want for each section, this code will give you a UITableView with as many sections as there are titles in self.monthTitle with 1 row in each section and a section header that correlates to that particular index of self.monthTitle. So if self.monthTitle holds all of the days of the week, your UITableView will look like this:
The following method returns the number of sections.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
So, if you need 3 sections, you have to return 3.
After that, you can use this method to name sections :
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
return #"name section 0";
else if(section == 1){
return #"name section 1";
}else
return #"name section 2";
//etc...
}
Use Array or dictionary to create dynamic structure
_sections = [NSMutableArray array];
[_sections addObject:#"section1"];
[_sections addObject:#"section2"];
_rows = [NSMutableArray array];
NSMutableArray* section1 = [NSMutableArray array];
[section1 addObject:#"row1"];
[section1 addObject:#"row2"];
NSMutableArray* section2 = [NSMutableArray array];
[section1 addObject:#"row1"];
[section1 addObject:#"row2"];
[_rows addObject:section1];
[_rows addObject:section2];
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_sections count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_sections objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[_rows objectAtIndex:section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
cell.textLabel.text = [[_rows objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
return cell;
}