UITableView inside UIPickerView in iOS - ios

I am struck at a problem where I need to implement a multiple type selection picker so for this purpose i am doing this :
caratFromPicker = [[UIPickerView alloc] init];
caratTable = [[UITableView alloc]initWithFrame:caratFromPicker.frame style:UITableViewStylePlain];
caratTable.delegate = self;
caratTable.dataSource = self;
caratTable.bounces = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-caratFromPicker.frame.size.height-50, self.view.frame.size.width, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
[toolBar setItems:toolbarItems];
price1.inputView = caratFromPicker;
price1.inputAccessoryView = toolBar;
[caratFromPicker setDataSource: self];
[caratFromPicker setDelegate: self];
caratFromPicker.showsSelectionIndicator = YES;//loadFromPicker
[caratFromPicker addSubview:caratTable];
and implemented the UITableView delegates as :
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [caratFromArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
cell.textLabel.text = [caratFromArray objectAtIndex:indexPath.row];
return cell;
}
here is the screenshot of the same :
but my problem is I am not able to scroll the tableview to view next values.

You can try using UIPicker delegate method :
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)theView{
return YourTableView;
}
To set tableView as the view for the row.

Related

UITableView unresponsive to gestures and not populating cells with data

I created a UITableView programmatically and set all of its attributes, but the data from my NSMutableArray will not populate the cells and the cells do nothing when tapped. Any insight appreciated, thanks!
Clayton
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
list = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * .3333, self.view.bounds.size.width, self.view.bounds.size.height * .6667) style:UITableViewStylePlain];
list.delegate = self;
list.dataSource = self;
list.backgroundColor = [UIColor magentaColor];
list.scrollEnabled = YES;
list.userInteractionEnabled = YES;
list.multipleTouchEnabled = YES;
list.hidden = NO;
[self.view addSubview:list];
[self.view bringSubviewToFront:list];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [favorites count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cell = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cell];
}
cell.textLabel.text = [[global sharedInstance].favorites objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.userInteractionEnabled = YES;
cell.hidden = NO;
cell.multipleTouchEnabled = YES;
[self.view bringSubviewToFront:cell];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"selected %ld row", (long)indexPath.row);
}
Got it thanks, the problem was with the data transferring from my singleton array of favorites to my local array (which was previously empty). Much obliged :)
At the end of viewDidLoad method, just add this:
[list reloadData];

How to pass data between two UITableViewControllers

I would like to pass data between two UITableViewControllers. In first UITableViewController I display NewPackageViewController which contains some cells like in code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section ==0)
{
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 310, 44)];
[textField setPlaceholder:#"Set package number here"];
[textField setEnablesReturnKeyAutomatically: YES];
[textField setReturnKeyType:UIReturnKeyDone];
[textField addTarget:self action:#selector(dismissKeyBoard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[textField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[aCell addSubview:textField];
}
else if(indexPath.section ==1){
aCell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
[aCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
aCell.textLabel.text = creationDate;
aCell.detailTextLabel.text = #"Choose";
aCell.imageView.image = [UIImage imageNamed:#"Date.png"] ;
}
else if(indexPath.section ==2)
{
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
aCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
aCell.textLabel.text = self.storeName;
aCell.imageView.image = [UIImage imageNamed:#"Buildings2020.png"];
aCell.detailTextLabel.text = #"Choose";
}
else if(indexPath.section ==4){
aCell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
aCell.textLabel.text = #"Add package item";
[aCell.textLabel setTextColor:[UIColor orangeColor]];
aCell.imageView.image = [UIImage imageNamed:#"New.png"];
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
aCell.backgroundColor = [UIColor clearColor];
}
else if(indexPath.section ==5){
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 100)];
[textView setEditable:YES];
[textView setFont:[UIFont fontWithName:#"Verdana" size:14]];
[textView setReturnKeyType:UIReturnKeyDone];
textView.delegate = self;
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
[aCell addSubview:textView];
}
return aCell;
}
After clicking on the section == 2 my app properly display second UITableView :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section)
{
case ...:
break;
........
case 2:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyBoard
instantiateViewControllerWithIdentifier:#"SelectStoreView"];
UINavigationController *navController = [[UINavigationController
alloc]initWithRootViewController:vc];
[self.navigationController presentViewController:navController animated:YES
completion:nil];
break; }
default:
break;
In SelectStoreViewController I implemented checkmarks
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayOfStores.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text= [arrayOfStores objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Buildings2020.png"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
}
In SelectStoreViewController I created delegate with method like below:
SelectStoreViewController.h
#class SelectStoreViewController;
#protocol PassStoreNameDelegate <NSObject>
-(void)setStoreName:(SelectStoreViewController*)controller
didFinishEnteringStoreName:(NSString*)enteredStoreName;
#end
#interface SelectStoreViewController :
UITableViewController<UITableViewDataSource,UITableViewDelegate>{
}
#property (retain,nonatomic) NSIndexPath *checkedIndexPath;
#property (strong,nonatomic) NSMutableArray *arrayOfStores ;
#property (nonatomic,weak) id<PassStoreNameDelegate>delegate;
#end
SelectStoreViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
arrayOfStores = [[NSMutableArray alloc]init];
[self fetchStoresFromDatabase];
self.title = #"Select store from list";
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Back"
style:UIBarButtonItemStylePlain target:self
action:#selector(didSelectBackBarButtonItem:)];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void)didSelectBackBarButtonItem:(id)sender{
[self.delegate setStoreName:self didFinishEnteringStoreName:#"store name"];
[self dismissViewControllerAnimated:YES completion:nil];
}
After that I implement PassStoreNameDelegate in NewPackageViewController (in my first UITableViewController)
NewPackageViewController.h
interface NewPackageViewController :
UITableViewController<UITextViewDelegate,PassStoreNameDelegate,CreationDateViewDelegate>
{
SelectStoreViewController *selectStoreNameViewController;
NSString *storeName;
}
#property (nonatomic, retain) SelectStoreViewController*selectStoreNameViewController;
#property (retain,nonatomic) NSString* storeName;
#end
NewPackageViewController.m
-(void)setStoreName:(SelectStoreViewController *)controllerdidFinishEnteringStoreName
(NSString *)enteredStoreName{
self.storeName = enteredStoreName;
[self.tableView reloadData];
}
When trying to perform this task I used this link:Passing Data between View Controllers
I would like to create functionality similar to radio button...
Every help will be appriciated
Ragards
First you should create radio button function in .h file. then implement your code in your 1st view controller.
The only thing you want to do is in 2nd view controller .h file import "secondviewController.h"
Then surely you can use that radio button function in secondviewController.m file

commitEditingStyle is not fired

I've a UITableView, I am trying to delete a row when editing mode is active but commitEditingStyle is not fired.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.text=[NSString stringWithFormat:#"Row Number %d",indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"trying to delete a row..");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 6;
}
-(void)Edit:(id)sender //Active editing mode
{
[self.table setEditing:YES animated:YES];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(Done:)];
}
I just want to delete a row?
This is how I show my popover:
UIPopoverController *popover;
-(IBAction)open:(id)sender
{
CGRect r=((UIButton*)sender).frame;
CGRect tRect=[((UIButton*)sender) convertRect:((UIButton*)sender).frame toView:self.view];
tRect.origin.x=r.origin.x;
Popover *firstViewCtrl = [[Popover alloc] init];
UINavigationController *navbar = [[UINavigationController alloc] initWithRootViewController:firstViewCtrl];
navbar.preferredContentSize = CGSizeMake(300, 300);
popover = [[UIPopoverController alloc] initWithContentViewController:navbar];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(300, 300);
CGRect popRect = CGRectMake(0,
0,
200,
200);
[popover presentPopoverFromRect:popRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
// [popover presentPopoverFromRect:tRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
I created UITableView using Xcode interface.
-(void)Done:(id)sender
{
[self.table setEditing:NO animated:NO];
//[self.table endEditing:true];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(Edit:)];
}
it fires when you touch DELETE button, not the minus... And delete buttons on your tableview doesn't show propably due to your tableview's width...
edit: after looking at your code, I think charty is correct.
be sure to check for the editing style, and make the necessary adjustments to your data source. something like:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"deleting");
[_resultsArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
}

ReloadData (UITableView) doesn't work

I got a big problem.
[self.tableView reloadData];
Doesn't work, and I don't understand why.
[[self tableView] reloadData];
Doesn't work too.
Here is my code:
.h
#interface ArticleViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
}
#end
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
btnLeft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnLeft"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(loadPlist)];
self.navigationItem.leftBarButtonItem = btnLeft;
In the loadPlist method, I'm writing in a .plist file. This part work fine.
Once all is write in the .plist file :
btnRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnRight"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(reloadTheTB)];
self.navigationItem.rightBarButtonItem = btnRight;
- (void)reloadTheTB {
NSLog(#"Test reloadTheTB");
[[self tableView] reloadData];
}
If I touch btnRight, I can see in the log "Test reloadTheTB".
Here is tableView:cellForRowAtIndexPath:
// 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 = [self getCellContentView:CellIdentifier];
contentDictio = [dict objectAtIndex:indexPath.row];
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
lblTemp1.text = [contentDictio objectForKey:#"title"];
if(indexPath.row % 2) {
UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_grise"]];
cell.backgroundView = myBackgroundView;
}
else {
UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_blanche"]];
cell.backgroundView = myBackgroundView;
}
}
return cell;
}
UPDATE:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dict count];
}
Help me please...
Actually I'm wondering this how you know that reloadData is not called?
Try to put some log in cellForRowAtIndexPath? to see if is called or not when you click your button? and also in do this :
- (void)reloadTheTB {
NSLog(#"Test reloadTheTB");
NSLog(#"%#",dict);
[[self tableView] reloadData];
}
Is the content of your dictionary what you expect? or is not changed after loadPlist?
Probably the call to reloadData is not executed on the main thread and you can update the UI only on the main thread. Try to execute your method on the main thread doing something like this :
-(void)ReloadTheTB{
[self.tableView performSelectorOnMainThread#selector(reloadData) withObject:nil waitUntilDone:NO]
}

UITableView clicking on an item an opening a new screen/view/activity

I have a table view(HomeViewController) consisting of items as:
locations
Reporting
Setting
I am having these items as separate files(LocationViewController,ReportingView Controller and Setting ViewController).Now if user clicks on location a new screen/activity/view should be open and same for rest items.
Can some one please help.my code is as:
#import "HomePageController.h" #implementation HomePageController
#synthesize menuList, table;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
menuList=[[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:#"LOCATIONS",nil],
[NSArray arrayWithObjects:#"REPORTING",nil],
[NSArray arrayWithObjects:#"SETTINGS",nil],
[NSArray arrayWithObjects:#"PASSWORD",nil],
[NSArray arrayWithObjects:#"HELP",nil],
[NSArray arrayWithObjects:#"ABOUT",nil],
[NSArray arrayWithObjects:#"SHARE",nil],
nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor=[UIColor blackColor];
self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;
self.title=#"CoinRead";
[table reloadData];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section{
return menuList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
}
cell.highlighted=NO;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSArray *rowArray = [menuList objectAtIndex:indexPath.row];
UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)]autorelease];
nameLabel.text = [NSString stringWithFormat:#"%#",[rowArray objectAtIndex:0]];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.shadowColor=[UIColor whiteColor];
nameLabel.shadowOffset=CGSizeMake(0.0, 0.5);
nameLabel.textColor = RGB(0,0,0);
[nameLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[cell.contentView addSubview:nameLabel];
return cell;
}
Implement below method of UITableViewDelegate to get the selection event
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Resources