Add UIViewcontroller with xib with UITableView in UIScrollView - objective-c

I'm trying to add a UIViewController in a UIScrollView, the UIViewController have a UITableView added with Interface Builder, i have connected it to data source and delegate to the File Owner's then i have connect the IBOutlet property with the ActivityView class, when i have do this to add to the UIScrollView:
ActivityView *viewActivity = [[ActivityView alloc] initWithNibName:#"ActivityView" bundle:nil];
[self.scrollView addSubview:viewActivity.view];
but when i open the view the app crash sometime with a BAD_EXC_ACCESS, sometime with a SIGBRT with this error:
[UILabel tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xd27f8d0
and sometime is this:
[SMPageControl tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xd68dcb0
and sometime is this:
[NSPathStore2 tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xd3746b0
i can't understand how ti fix it, if i use the ActivityView alone, without adding it to the UIScrollview, works perfectly, and if i add ActivityView without the connection with the UITableView works, so i think the problem is adding it to the UIScrollView with the UITableView outlet connection, how i can do it?
this is the code of ActivityView:
#import "ActivityView.h"
#interface ActivityView () <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) NSMutableArray *activities;
#end
#implementation ActivityView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.activities 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.activities objectAtIndex:indexPath.row];
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.activities = [NSMutableArray array];
[self.activities addObject:#"row"];
}
EDIT: i have tried also to add the UITableView programmatically, without interface builder, and when i set the datasource delegate, crash...everyone can try...crash always...

Related

Custom TableView-Cell (Data from server) select multiple values with accessory change

I am working with TableView and wanted Custom TableViewCell to have a Small Image, Name and one custom image (With Tickmark and without Tick) can be on accessory to show if Cell is selected and if it's not selected it will show without Tick image on unselected cells.
And if i want to select multiple cells then it should show Tick image on selected Cells and Untick image on unselected cells and after that when i click on a button then i should be able to get the selected cell id's.
On the tableView i am getting all the values from the server and images also from URL's but the Tickmark and Unselected Tick mark image will be used the project itself.
So far i have created :
Class .h,.m,.xib of "ResultTableCell" of type UITableViewCell and my Main view "Result" with the TableView and a Button on top (on click of button i'll get the values of selected cells)
ResultTableCell.h
#import <UIKit/UIKit.h>
#interface ResultTableCell : UITableViewCell
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UIImageView *thumbImageView;
ResultTableCell.m
#import "ResultTableCell.h"
#implementation ResultTableCell
#synthesize nameLabel,thumbImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
ResultTableCell.xib
The right hand side image on the xib is the place where the accessor image will come.
ResultTableCell.xib
And the main xib
Result.h
#import <UIKit/UIKit.h>
#import "ASIFormDataRequest.h"
#interface Results : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *nameData;
}
#property (nonatomic, retain)NSMutableArray *nameData;
#property (nonatomic, retain)NSMutableArray *ImageData;
#property (nonatomic, retain)NSMutableArray *idData;
#property (nonatomic, retain)UITableView *table;
#property (nonatomic, retain) IBOutlet UIButton *done;
#property (nonatomic, retain) NSMutableArray *arFors;
-(IBAction)save_done:(id)sender;
Result.m
#import "Results.h"
#import "ResultTableCell.h"
#interface Results ()
#end
#implementation Results
#synthesize arFors;
#synthesize done,nameData,table,addressData,ImageData,idData;
- (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 from its nib.
self.arFors=[NSMutableArray array];
// I am Getting Name,id and image url data from my HomeViewController
NSLog(#"Name Data from home view is %#",nameData); // 10 Names get's printed in log
NSLog(#"id Data is %#",idData);
NSLog(#"URL image data is %#",ImageData);
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 221, 320, 327) style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Name data count is %d",nameData.count);
return nameData.count;
//return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}*/
static NSString *simpleTableIdentifier = #"ResultTableCell";
ResultTableCell *cell = (ResultTableCell *)[table dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ResultTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) {
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"table_tick" ]];
}
else {
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"table_add" ]];
}
NSLog(#"data is ************* %#",nameData);
cell.nameLabel.text = [nameData objectAtIndex:indexPath.row];
NSURL * imageURL = [NSURL URLWithString:[ImageData objectAtIndex:indexPath.row]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image2 = [UIImage imageWithData:imageData];
cell.ImageView.image = image2;
cell.ImageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didSelectRowAtIndexPath %d",indexPath.row);
if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) {
[self.arFors removeObject:[NSNumber numberWithInt:indexPath.row]];
}
else{
[self.arFors addObject:[NSNumber numberWithInt:indexPath.row]];
// [self.table cellForRowAtIndexPath:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadData];
}
-(IBAction)save_done:(id)sender
{
NSLog(#"selected cell values are %#",self.arFors);
}
Now with this code everything is working fine (tick image is shown on selected cells and untick image on unslected cells and on clicking the Done button i am getting the selected cell values),
But the Problem comes when i tapp on a cell then it like hangs and takes 5-6 seconds of time to change accessor image as it fires [tableView reloadData] in didselectrowatindexpath method so all data reloads again in the tableview and then the accessor image changes, please can any one correct my code or enhance it so that it works fast.
I have tried a lot of ways but i was not able to do it without the reloading of table and if i reload table it takes long time.
Coding help will be much tankful.
Your problem is:
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
Because it downloads all of the image data from the network each time you reload the table view. You should be doing this asynchronously and caching the returned image so you don't need to download it repeatedly. Take a look at a library like SDWebImage to help you with this.

Adding UITableView to existing UIViewController

I have a UIViewController which open up a Camera, after doing the imagePickerController:didFinishPickingMediaWithInfo: a UITableView shall be shown.
Somehow the tableView delegation code is not triggered. A tableView is shown on my iPhone but with empty rows. Seems that just the IB Stuff is displayed without the code. But I cannot figure out what I missed.
How do I get the UITableView working?
MainStoryboard.storyboard
Camera View Controller Scene
----------------------------
Camera View Controller
View
Image View // hooked up with .h
Table View // hooked up with .h
Table View Cell - clocation
CameraViewController.h
#import <UIKit/UIKit.h>
#interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *imageView; // hooked up with IB UIImageView
#property (strong, nonatomic) NSMutableArray *locations;
#property (strong, nonatomic) IBOutlet UITableView *tableView; // hooked up with IB UITableView
#end
CameraViewController.m (Updated 20130725 with working solution)
#import "CameraViewController.h"
#interface CameraViewController ()
#end
#implementation CameraViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// self.locations = [[NSMutableArray alloc] init];
// is filled with content when photo is taken. Means has content when tableview shall be shown.
self.locations = #[ #"foo", #"bar" ]; // dummy setup for this thread
// Do any additional setup after loading the view.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// select Photo
} else {
[self takePhoto];
}
[self initTableView];
}
// [...] Camera Stuff
#pragma mark - table view things
-(void)initTableView {
_tableView.dataSource = self;
_tableView.delegate = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.locations.count; // a breakpoint is configured but never get hit...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"clocation";
// Not working in this case:
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// You have to use this one:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"Empty Cell %d", indexPath.row];
return cell;
}

TableView Delegate Messages Not Being Called

SOLVED: Not really a solution, but I just used a .xib rather than trying to use the storyboard thing. Whatever I was trying to do with the storyboard was somehow messing up the TableView delegate methods from being called. Thanks for all the insight you guys provided.
I have a UIViewController that contains a UITableView that should be populated by an array.
Unfortunately the UITableView does not get populated by the array (which is not empty).
My .h file:
#interface Directory : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {
NSArray *listData;
NSArray *savedListData;
MKMapView* mapUI;
UISearchBar* searchBar;
NSArray * originalListData;
UITableView* placeList;
DirInfoListing *infoInst;
NSString *searchInit;
NSString *openInit;
NSMutableArray *buildings;}
#property (nonatomic, retain) NSArray *listData;
#property (nonatomic, retain) NSArray *savedListData;
#property (nonatomic, retain) IBOutlet UISearchBar* searchBar;
#property (nonatomic, retain) IBOutlet UITableView* placeList;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil list: (NSArray*)array mapView:(MKMapView*) map;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil list:(NSArray*)array mapView:(MKMapView*) map search:(NSString*) ss;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil list: (NSArray*)array mapView:(MKMapView*) map open:(NSString*) openTx;
-(void)doDBLoad;
#end
And relevant stuff from my .m file:
#import "Directory.h"
#interface Directory ()
#end
#implementation Directory
#synthesize listData, savedListData;
#synthesize searchBar;
#synthesize placeList;
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"ListData count: %lu", (unsigned long)savedListData.count);
//This returns a valid number of 74 items
return [self.savedListData count];
}
/*
* Populates the table with list data and provides title. AND DOES NOT GET CALLED
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tID = #"tablIDDirect";
UITableViewCell *c = [tableView dequeueReusableCellWithIdentifier:tID];
if(c == nil) {
c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tID];
}
NSInteger r = [indexPath row];
if([self.listData objectAtIndex:r] != nil){
c.textLabel.text = ((DBBuilding*)[self.savedListData objectAtIndex:r]).Name;
c.detailTextLabel.text = ((DBBuilding*)[self.savedListData objectAtIndex:r]).Acronym;
}
c.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return c;
}
/*
* Displays an entry according to the table selection and redirects the user to the Directory Information Listing.
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger r = [indexPath row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(infoInst != nil) {
infoInst = nil;
}
infoInst = [[DirInfoListing alloc] initWithNibName:#"DirInfoListing" bundle:nil building:[self.savedListData objectAtIndex:r] map:mapUI];
}
/*
* Notification that the Directory is the active view
*/
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(openInit != nil)
{
[self openEntry:openInit];
openInit = nil;
}
placeList = [[UITableView alloc] init];
placeList.dataSource = self;
placeList.delegate = self;
[placeList reloadData];
}
/*
* Notification that the Directory is no longer the active view
*/
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)viewDidLoad
{
NSLog(#"ListData count: %lu", (unsigned long)listData.count);
//Also returns a valid number of 74 objects within the array
savedListData = [[NSArray alloc] initWithArray:listData];
//That get put into another array just for my sanity
[super viewDidLoad];
// Do any additional setup after loading the view.
searchBar.text = searchInit;
[self performSearch:searchInit];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionmethod gets called, but none of the other UITableView delegate methods are called and I am at a loss as to why this happens.
Also, this whole chunk of code is pulled from another one of my apps (That works correctly) using .xibs and DOES NOT use storyboards. This app (the one I'm having problems with) uses storyboards, which I am not very familiar with. I may be doing something wrong with calling and using the storyboard, but I am just not sure. If there may be something related to that I can provide more information.
Could you guys please go over my code and maybe see where I've gone wrong?
Thanks!
In your "viewDidLoad" method, change one line to this:
self.savedListData = [[NSArray alloc] initWithArray:listData];
Except "listData" needs to be something. "self.listData" might be that something, but on my first pass, I do not see where "self.listData" gets populated or set.
With properties, you usually use "self." to access (set and get) everything for the properties everywhere except in the init methods.
The reason the table is not displaying anything is because your "numberOfRowsInSection" method is likely returning zero. Set a breakpoint and check that.

IOS 5.1 ARC Unrecognized Selector Sent to Instance

I have been looking at similar question but couldn't figure it out what is the problem.It seems it should work but it gives me error.
In IOS 5.1 Ipad Stortyboard application I have a right navigation bar item when user click a popover view should open. I had a working popover view but design was not good so I replaced it with a new popover class now it gives me following error
-[UIButton view]: unrecognized selector sent to instance 0xa17ba80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton view]: unrecognized selector sent to instance 0xa17ba80'
I have tried following functions but none has worked so far. It gives me similar errors when I change the code.
- (IBAction)setColorButtonTapped:(id)sender{
- (void)setColorButtonTapped:(id)sender{
- (IBAction)setColorButtonTapped:(id)sender forEvent:(UIEvent*)event {
- (void)setColorButtonTapped:(id)sender forEvent:(UIEvent*)event {
and ofcourse I have changed ti following regarding to ibaction or void
[backButton2 addTarget:self action:#selector(setColorButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
here is the code
my.h file
#import <UIKit/UIKit.h>
#import "ColorPickerController.h"
#interface MeetingViewController : UITableViewController<UIApplicationDelegate,UIAlertViewDelegate,DropDownListDelegate,MFMailComposeViewControllerDelegate,EGORefreshTableHeaderDelegate,ColorPickerDelegate>{
UIPopoverController *_popover;
ColorPickerController *_colorPicker;
UIPopoverController *_colorPickerPopover;
}
#property (nonatomic, strong) UIPopoverController *popover;
#property (nonatomic, strong) ColorPickerController *colorPicker;
#property (nonatomic, strong) UIPopoverController *colorPickerPopover;
- (IBAction)setColorButtonTapped:(id)sender;
#end
my.m file
#synthesize popover = _popover;
#synthesize colorPicker = _colorPicker;
#synthesize colorPickerPopover = _colorPickerPopover;
- (void)viewDidLoad
{
[super viewDidLoad];
//gear button on navigation Bar
UIImage* imageback2 = [UIImage imageNamed:#"ICON - Gear#2x.png"];
CGRect frameimgback2 = CGRectMake(0, 0, 40, 40);
UIButton *backButton2 = [[UIButton alloc] initWithFrame:frameimgback2];
[backButton2 setBackgroundImage:imageback2 forState:UIControlStateNormal];
[backButton2 addTarget:self
action:#selector(setColorButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:backButton2];
self.navigationItem.rightBarButtonItem = btn2;
}
#pragma mark ColorPickerDelegate
- (void)colorSelected:(NSString *)color {
[self.colorPickerPopover dismissPopoverAnimated:YES];
}
#pragma mark Callbacks
- (IBAction)setColorButtonTapped:(id)sender {
if (_colorPicker == nil) {
self.colorPicker = [[ColorPickerController alloc] initWithStyle:UITableViewStylePlain];
_colorPicker.delegate = self;
self.colorPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_colorPicker];
}
[self.colorPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
utility class
ColorPickerController.h
#import <UIKit/UIKit.h>
#protocol ColorPickerDelegate
- (void)colorSelected:(NSString *)color;
#end
#interface ColorPickerController : UITableViewController {
NSMutableArray *_colors;
id<ColorPickerDelegate> __weak _delegate;
}
#property (nonatomic, strong) NSMutableArray *colors;
#property (nonatomic, weak) id<ColorPickerDelegate> delegate;
#end
utilityclass
ColorPickerController.m
#import "ColorPickerController.h"
#implementation ColorPickerController
#synthesize colors = _colors;
#synthesize delegate = _delegate;
#pragma mark -
#pragma mark Initialization
/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
if ((self = [super initWithStyle:style])) {
}
return self;
}
*/
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(150.0, 140.0);
self.colors = [NSMutableArray array];
[_colors addObject:#"Red"];
[_colors addObject:#"Green"];
[_colors addObject:#"Blue"];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
return YES;
}
#pragma mark -
#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 [_colors count];
}
// Customize the appearance of 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];
}
// Configure the cell...
NSString *color = [_colors objectAtIndex:indexPath.row];
cell.textLabel.text = color;
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_delegate != nil) {
NSString *color = [_colors objectAtIndex:indexPath.row];
[_delegate colorSelected:color];
}
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
self.delegate = nil;
}
#end
Help is much appreciated , thanks
You're using a UIButton as a customView for a UIBarButtonItem. This could be the problem.
I suggest you use UIBarButtonItem's initWithImage:style:target:action: initializer instead.
UIButton is a view and as such has no view property or instance method.

Table View Controller and TextField Inside ModalView

I try to add tableView and TextField inside the ModalView. I do so. I create new View Controller and give for it
#import <UIKit/UIKit.h>
#protocol UYLModalViewControllerDelegate
-(void) buttonDonePassed :(NSArray *) variables;
#end
#interface UYLModalViewController : UIViewController <UITableViewDelegate>
{
id<UYLModalViewControllerDelegate> delegate;
IBOutlet UITableView *tblView;
IBOutlet UITextField *textField;
NSMutableArray *cellsArray;
//UITextField *textField;
}
#property (nonatomic, assign) id<UYLModalViewControllerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UITableView *tblView;
#property (retain, nonatomic) IBOutlet UITextField *textField;
#end
And IN .m File I create functions
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cellsArray 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] autorelease];
}
cell.textLabel.text = [cellsArray objectAtIndex:indexPath.row];
// Configure the cell.
return cell;
}
and ViewDidiLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(buttonPassed:)];
//UITableView *tableView = [[UITableView alloc] init];
//[self]
cellsArray = [[NSMutableArray alloc] initWithObjects:#"one",#"two",#"three", nil];
[tblView reloadData];
}
But my program don't go to the TableViewDelegate methods (such as cellforrowAtIndexPath)
You need to set your UYLModalViewController as the tableView's delegate and datasource.
It looks like you are using Interface Builder so you need to:
control + click on the tableView and drag it across to the File's Owner
Then select datasource from the HUD menu.
Then repeat the process but selecting delegate
Note
Your controller should also conform to UITableViewDatasource giving you:
#interface UYLModalViewController : UIViewController <UITableViewDelegate, UITableViewDatasource>
1.
2.
If you prefer you can do this in code in viewDidLoad or any place where you create the tableView if you do it programatically.
self.tableView.delegate = self;
self.tableView.datasource = self;