Cells Do Not Appear in UITableViewController - ios

I'm having an issue with displaying cells in my UITableViewController, simply..nothing shows up when I go to test. Below is my code.
FormationViewController.m
#import <Foundation/Foundation.h>
#import "FormationViewController.h"
#interface FormationViewController ()
#end
#implementation FormationViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Title"
forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Title"];
}
NSString *name = _names[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", name];
return cell;
}
- (void) tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Formation View";
self.tabBarItem.image = [UIImage imageNamed:#"tab_icon_formation"];
self.names = #[#"John Doe", #"Lee Harvey Oswald", #"Pat Buchanan", #"Angry Orchard",
#"Sierra Nevad", #"Jeff Bridges", #"John McClain", #"Lucy Genero"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"The length of names is %lu", self.names.count);
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.formationVC.frame = CGRectMake(
0,
self.topLayoutGuide.length,
CGRectGetWidth(self.view.frame),
CGRectGetHeight(self.view.frame)
- self.topLayoutGuide.length
- self.bottomLayoutGuide.length
);
}
- (void)loadView {
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];
self.formationVC = [[UIView alloc] init];
self.formationVC.backgroundColor = [UIColor whiteColor];
[view addSubview:self.formationVC];
self.view = view;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
#end
And my FormationViewController.h
#import <UIKit/UIKit.h>
#interface FormationViewController : UITableViewController
#property (strong, nonatomic) UIView *formationVC;
#property (strong, nonatomic) NSArray *names;
#end
I'm also not sure if I'm putting some of these lines of code in the right methods.
I also think that my problem might be lying in the tableView:cellForRowAtIndexPath: method. I'm not absolutely sure I'm implementing that 100% correctly.
If anyone can help me find out what I'm doing wrong please let me know. This is my first Obj-C/iOS project I'm working on too so please be gentle! Thank you!

Well, I tried your code , using no xibs and faced the same problem.
Then I changed 2 things
removed LoadView.
moved self.names intialization from initWithNibName to viewDidLoad
added
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Title"];
[self.tableView reloadData];
to viewDidload
And it worked

I think you' re never stop in (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil ... if you instantiate your view controller from a storyboard, you need to implement initWithCoder, or just put your names initialization in viewDidLoad.

As you are probably using Storyboard, check these steps:
1) Storyboard - select your table view controller and in Identity Inspector make sure you have it's class set to your FormationVC.
2) Storyboard - select table view inside the controller and select it's first cell (suggesting you use Prototype Cells). Make sure it's style is set to Basic and set it's identifier for example to "BasicCell".
3) Go to your code and use this:
FormationVC.h
#import <UIKit/UIKit.h>
#interface FormationVC : UITableViewController
#end
FormationVC.m
#import "FormationVC.h"
#interface FormationVC ()
#property (strong, nonatomic) NSArray *names;
#end
#implementation FormationVC
#pragma mark - View Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// you must not forget to call your custom init method once the view is loaded
[self setupView];
}
- (void)setupView {
// set title of VC
self.title = #"Formation View";
// create array of names
_names = #[#"John Doe", #"Lee Harvey Oswald", #"Pat Buchanan", #"Angry Orchard",
#"Sierra Nevad", #"Jeff Bridges", #"John McClain", #"Lucy Genero"];
// log number of names in names array
NSLog(#"The length of names is %lu", _names.count);
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BasicCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%#", _names[indexPath.row]];
return cell;
}
#end

Problem IS there :-
#property (strong, nonatomic) NSArray *names;
change NSArrayTo NSMutableArray
in.h
EX:- #property (nonatomic , retain) NSMutableArray *names
and in.m
names = [[NSMutableArray alloc] init];
and then alloc init your Array and then put value its work .

Related

UITableViewCell Subtitle not showing up

I have my UITableView displaying JSON info being returned from a cloud code function. For some reason, it correctly displays the titles of the items being returned, but I can't get it to display the price as a subtitle of each cell. Setting the style as UITableViewCellStyleSubtitle doesn't seem to work, and gives me a warning stating Implicit conversion from enumeration type 'enum UITableviewCellStyle' to different enumeration type.
MatchCenterViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"
#interface MatchCenterViewController : UIViewController <UITableViewDataSource>
#property (nonatomic) IBOutlet NSString *itemSearch;
#property (nonatomic, strong) NSArray *imageURLs;
#property (strong, nonatomic) NSString *matchingCategoryCondition;
#property (strong, nonatomic) NSString *matchingCategoryLocation;
#property (strong, nonatomic) NSNumber *matchingCategoryMaxPrice;
#property (strong, nonatomic) NSNumber *matchingCategoryMinPrice;
#property (strong, nonatomic) NSArray *matchCenterArray;
#end
MatchCenterViewController.m:
#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>
#interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) UITableView *matchCenter;
#end
#implementation MatchCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
// _matchCenter.dataSource = self;
// _matchCenter.delegate = self;
// [self.view addSubview:self.matchCenter];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-200);
_matchCenter.dataSource = self;
_matchCenter.delegate = self;
[self.view addSubview:self.matchCenter];
self.matchCenterArray = [[NSArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
self.matchCenterArray = [[NSArray alloc] init];
[PFCloud callFunctionInBackground:#"MatchCenterTest"
withParameters:#{
#"test": #"Hi",
}
block:^(NSDictionary *result, NSError *error) {
if (!error) {
self.matchCenterArray = [result objectForKey:#"Top 3"];
dispatch_async(dispatch_get_main_queue(), ^{
[_matchCenter reloadData];
});
NSLog(#"Test Result: '%#'", result);
}
}];
[self.matchCenter registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.matchCenterArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *matchCenterDictionary= [self.matchCenterArray objectAtIndex:indexPath.row];
cell.textLabel.text = [matchCenterDictionary objectForKey:#"Title"];// title of the item
cell.detailTextLabel.text = [matchCenterDictionary objectForKey:#"Price"];// price of the item
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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.
}
*/
#end
The cell you have registered is a default styled UITableViewCell, it does not have a subTitle. You can't change the style of the cell once it's created.
You basically have two options:
create a simple UITableViewCell subclass that uses UITableViewCellStyleSubtitle (or any other style of your choice)
#interface MyTableViewCell : UITableViewCell
#end
#implementation MyTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
// overwrite style
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
return self;
}
#end
...
[self.matchCenter registerClass:[MyTableViewCell class] forCellReuseIdentifier:#"Cell"];
Or return to the old style dequeue technique where you create a cell if none could be dequeued
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
/* configure */
}
If you do this, you have to remove the registerClass:forCellReuseIdentifier: call.
I would go with option one, because most likely pretty soon you will figure out that the built in cells are very limited and you want to add your own views (e.g. labels, image views). And if you use a subclass you can use properties to access those views, and don't have to use hacks like tags to access them.
In Table View subtitle is not present otherwise you customize your cell
use detail text label
cell.detailTextLabel.text = [array objectForKey:#"Value"];

View flashes when popToRootViewController

I am having a problem with popToRootViewController. I have a root view A and a table view B. And when a row is selected in the table view I pass a string back to the root view and depending on the string. I change the title of buttons on A.
I have made a very simple version of this and put it on gitHub at this link: https://github.com/spennyf/didSelect_test. I am doing this because it is very hard to explain until you actually run this on your phone of the simulator. And see the flash. I don't know why it is happening or how to fix it. Any help would be greatly appreciated I will also post most of the code below, but if you could see the flash for yourself I think it would help explain the problem. Heres the code:
viewControllerA.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *btn1;
#property (strong, nonatomic) IBOutlet UIButton *btn2;
#property (strong, nonatomic) IBOutlet NSString *object;
#end
viewControllerA.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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated {
NSLog(#"object: %#", _object);
if ([_object isEqualToString:#"object1"]) {
[_btn1 setTitle:#"new1" forState:UIControlStateNormal];
}
if ([_object isEqualToString:#"object2"]) {
[_btn2 setTitle:#"new2" forState:UIControlStateNormal];
}
}
#end
tableviewB.m
#import "TableViewController.h"
#import "ViewController.h"
#interface TableViewController () {
NSMutableArray *_objects;
}
#end
#implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_objects = [[NSMutableArray alloc] init];
NSDictionary *obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:#"object1", #"title", nil];
NSDictionary *obj2 = [[NSDictionary alloc] initWithObjectsAndKeys:#"object2", #"title", nil];
// NSDictionary *obj3 = [[NSDictionary alloc] initWithObjectsAndKeys:#"rpi", #"title", nil];
[_objects addObject:obj1];
[_objects addObject:obj2];
}
#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 _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//NSLog(#"%#", _objects);
cell.textLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:#"title"];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ViewController *myController = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0];
myController.object = [[_objects objectAtIndex:indexPath.row] objectForKey:#"title"];
[self.navigationController popToRootViewControllerAnimated:YES];
}
#end
Again thanks for the help.
There is no problem with your code. It is intended behavior for UIButtons of type UIButtonTypeSystem to flash when its title is changed.
A simple workaround is to set the type of your buttons to UIButtonTypeCustom.
Edit:
You can change the type of the button in Interface Builder:

Adding a UITableView as a Subview

I am trying to create a view that has a UITableView as only part of that view. I believe this is the correct pattern when creating from code (not interface builder) but please feel free to add suggestions if my approach is wrong as well.
The exception I am getting is:
[KBSMoreViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
I have a class header as below (I am implementing the constructor in the implementation):
#import <UIKit/UIKit.h>
#interface KBSMoreTableView : UITableView
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
#end
I then have a ViewController class header as:
#interface KBSMoreViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#end
The ViewController, which is part of a tab bar and works fine (outside of the tableview I am trying to add) implementation is written as:
#import "../Models/KBSMoreTableView.h"
#interface KBSMoreViewController ()
#property (strong, nonatomic) KBSMoreTableView* tableView;
#property (strong, nonatomic) NSString* cellIdentifier;
#property (copy, nonatomic) NSArray *source;
#end
#implementation KBSMoreViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:0];
self.source = [[NSArray alloc] initWithObjects:#"Test1", #"Test2", nil];
self.cellIdentifier = #"MoreCellId";
}
return self;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return self.source.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cellIdentifier];
cell.textLabel.text = self.source[indexPath.row];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[KBSMoreTableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
}
The error is very clear. You don't implement the tableView:numberOfRowsInSection: table view data source method. Instead, you have created a method named numberOfRowsInSection:.
Change this:
- (NSInteger)numberOfRowsInSection:(NSInteger)section
to:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Having trouble passing NNString label name from MasterView to DetailView

I'm trying to make a Master-Detail app that displays a list of Sounds on the master view. When you tap the sound, you go to the detail screen, and it displays the Sound you chose as a label, then shows a short movie of the sound. My problem is, all the sounds show as the label the name of the first sound in the list. When I tap on Sound B, the detail screen shows "Sound B." When I tap on tSound F, the detail screen shows "Sound B."
No errors or warnings come up. Any ideas?
Thanks in advance.
slfViewController.h
#import <UIKit/UIKit.h>
#interface slfViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, strong) IBOutlet UITableView *tableView;
#end
slfViewController.m
#import "slfViewController.h"
#import "slfSoundDetailViewController.h"
#interface slfViewController ()
#end
#implementation slfViewController
{
NSArray *tableData;
NSArray *thumbnails;
}
#synthesize tableView = _tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableData = [NSArray arrayWithObjects:
#"B Sound",
#"Ch Sound",
#"D Sound",
#"F Sound",
...
#"Zh Sound",
nil];
thumbnails = [NSArray arrayWithObjects:
#"b.jpg",
#"ch.jpg",
#"d.jpg",
#"f.jpg",
...
#"zh.jpg",
nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showSoundDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
slfSoundDetailViewController *destViewController = segue.destinationViewController;
destViewController.soundName = [tableData objectAtIndex:indexPath.row];
}
}
#pragma mark - TableView Data Source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"soundCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
return cell;
}
#end
slfSoundDetailViewController.h
#import <UIKit/UIKit.h>
#interface slfSoundDetailViewController : UIViewController
#property (nonatomic, strong) IBOutlet UILabel *soundLabel;
#property (nonatomic, strong) NSString *soundName;
#end
slfSoundDetailViewController.m
#import "slfSoundDetailViewController.h"
#interface slfSoundDetailViewController ()
#end
#implementation slfSoundDetailViewController
#synthesize soundLabel;
#synthesize soundName;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
soundLabel.text = soundName;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
You’re only setting soundLabel.text = soundName in viewDidLoad… so it’s only being called once (when the view controller’s view is first loaded). This app is most likely reusing the same instance of slfSoundDetailViewController so it doesn’t reload its view.
You’ll probably want to override either -[slfSoundDetailViewController setSoundName:] or -[slfSoundDetailViewController viewWillAppea:] and set soundLabel.text there.

iOS reload UITableView using UISegmentedControl

Hi I'm trying to reload my table view based off of two different arrays. Which array should be loaded is determined by a segment control in the navigation bar. Currently it only will load the first array and nothing happens when the segment control is pressed. Below is my code any help as to why this isn't working is greatly appreciated. I've also checked that my IBAction segmenter is connected in the nib.
MessageViewController.h
#import <UIKit/UIKit.h>
#interface MessageViewController : UIViewController<UITableViewDelegate> {
IBOutlet UISegmentedControl *segmentControl;
IBOutlet UINavigationBar *navBar;
IBOutlet UITableView *tableView;
}
#property (retain, nonatomic) IBOutlet UISegmentedControl *segmentControl;
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#property (nonatomic, retain) NSMutableArray *inbox;
#property (nonatomic, retain) NSMutableArray *sent;
#end
MessageViewController.m
#import "MessageViewController.h"
#interface MessageViewController () <UITableViewDelegate>
#end
#implementation MessageViewController
#synthesize segmentControl;
#synthesize inbox;
#synthesize sent;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.title = NSLocalizedString(#"Messages", #"Messages");
self.tabBarItem.image = [UIImage imageNamed:#"mail_2_icon&32"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.inbox = [NSMutableArray arrayWithObjects:#"testing", #"test", #"another", nil];
self.sent = [NSMutableArray arrayWithObjects:#"test", #"another", #"testing", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(segmentControl.selectedSegmentIndex == 0){
return [inbox count];
}else if(segmentControl.selectedSegmentIndex == 1){
return [sent count];
}else{
return [inbox count];
}
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(segmentControl.selectedSegmentIndex == 0){
NSString *cellValue = [inbox objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}else if(segmentControl.selectedSegmentIndex == 1){
NSString *cellValue = [sent objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}else{
NSString *cellValue = [inbox objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
return cell;
}
-(IBAction)segmenter{
[tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
[inbox release];
[sent release];
[segmentControl release];
[segmentControl release];
[super dealloc];
}
- (void)viewDidUnload {
[self setSegmentControl:nil];
[segmentControl release];
segmentControl = nil;
[super viewDidUnload];
}
#end
Not sure why it wasn't working in the end all I did was delete the three classes and redo everything with the code above something must have just got borked along the way but it's working now and I'm a happy camper. Didn't need the delegate stuff either since that's all done in the nib so my original code worked fine.
set the delegate and datasource methods and take breakpoint to check the data . your code is right .
tableview.delegate=self;
tableView.datasource=self;
The only problem I see is that you're using a view controller with a table view in it, but you're not setting up the delegate for it in your viewDidLoad, and you're not implementing the delegate for your view controller.
#interface MessageViewController () <UITableViewDelegate, UITableViewDataSource>
#end
In viewDidLoad:
self.tableView.delegate = self;
self.tableView.dataSource = self;
Also make sure you have everything hooked up properly in IB, both your segmented control and your table view.
EDIT: I made my own test as per what you're trying to do, Here's my own implementation file

Resources