I saw many other questions like this, but I didn't find the (right) answer. I've got two UITableViews on two different UIViewControllers. But the second UITableView doesn't show any rows.
Code for the second UIViewController (detail.m):
// detail.m
#import "detail.h"
#interface detail ()
#end
#implementation detail {
}
#synthesize tweetsArray;
#synthesize tableView;
#synthesize searchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
//1
self.tableView.dataSource = self;
//2
self.tweetsArray = [[NSArray alloc] initWithObjects:
#"Always put your fears behind you and your dreams in front of you.",
#"A relationship with no trust is like a cell phone with no service, all you can do is play games.",
#"People should stop talking about their problem and start thinking about the solution.",
#"Dear Chuck Norris, Screw you. I can grill burgers under water. Sincerely, Spongebob Squarepants.",
#"My arms will always be open for you, they will never close, not unless you're in them.",
nil];
}
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tweetsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//5
static NSString *cellIdentifier = #"SettingsCell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//5.1 you do not need this if you have set SettingsCell as identifier in the storyboard (else you can remove the comments on this code)
//if (cell == nil)
// {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
// }
//6
NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:#"via Codigator"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I dont see you setting the delegate for the tableview. You need to set that
self.tableView.delegate = self;
Related
I need to use a UITextView to enter in information, which then moves the information into a UITableView.
I need it to update the tableview each time a new line has been added.
I thought i could create an array out of the information in the TextView and place it into a TableView but i cant seem to figure out how to do this.
With this it still doesn't populate the table with what i type in the 'UITextView'.
-(void)textViewDidChange:(UITextView *)textView{
listArray=[[NSMutableArray alloc]initWithArray:[textView.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
[self.tableView reloadData];
}
- (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 = [listArray objectAtIndex:indexPath.row];
return cell;
}
First set the delegate property of the UITextView to your viewController. Then use textViewDidChange method to initialize your array--
in ViewController.h file-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UITextView *txtView;
#end
and in ViewController.m file
#import "ViewController.h"
#interface ViewController (){
NSMutableArray *listArray;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.txtView.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(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:#"%#",[listArray objectAtIndex:indexPath.row]];;
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return listArray.count;
}
-(void)textViewDidChange:(UITextView *)textView{
listArray=[[NSMutableArray alloc]initWithArray:[textView.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
[self.tableView reloadData];
NSLog(#"Array-- %#",listArray);
NSLog(#"Array Count-- %lu",(unsigned long)listArray.count);
NSLog(#"Text-- %#",textView.text);
}
#end
Use listArray to show your data on tableView.
You can create an array with the text of textview using the following method :
NSString* string;
NSArray* array = [string componentsSeparatedByString:(nonnull NSString*)]
now you can create your tableview using the above array. whenever you want to update the table, you can call the following method :
[tableView reloadData];
If you just want to look for new lines you can do something like this:
NSArray <NSString*> *components = [self.textView.text componentsSeparatedByString:#"\n"];
If you implement the delegate methods of UITextView specifically to get notifications when the text content changes you can then update an array that feeds into your table view.
Also here's the code in Swift just because we should be thinking about it now.
let components: [String] = textView.text.componentsSeparatedByString("\n")
I feel like this is a fairly easy question, but I've tried for hours and can't seem to get it. I'm trying to create an app that calculates different equations that are critical to my job. Essentially, I've grouped the calculations into categories using nested TableViewControllers. This part works.
The part I am trying to figure out is how to push to a UIViewController so I can actually have the user enter data to do the calculations. For some reason I can't seem to get it to work. I am using Xcode 5 and am using Storyboards.
Really sorry for the long post. I've also included a screenshot of the storyboard at the following link: http://imgur.com/0oSxsR1
code below:
Root TableViewController Code (RootTableViewController.m)
#import "RootTableViewController.h"
#import "calculatorTableViewController.h"
#interface RootTableViewController ()
#end
#implementation RootTableViewController{
NSArray *calculatorOptions;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
calculatorOptions = [NSArray arrayWithObjects: #"General", #"Digital", #"Print", #"Television", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [calculatorOptions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CalculatorCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [calculatorOptions objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"calculatorDetails"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
calculatorTableViewController *destViewController = segue.destinationViewController;
destViewController.calculationType = [calculatorOptions objectAtIndex:indexPath.row];
destViewController.title = destViewController.calculationType;
}
}
#end
Secondary TableViewController.h file (calculatorTableViewController.h)
#import <UIKit/UIKit.h>
#interface calculatorTableViewController : UITableViewController
#property (nonatomic, strong) NSString *calculationType;
#end
Secondary TableViewController.m file (calculatorTableViewController.m)
#import "calculatorTableViewController.h"
#import "CalculateGrpsHaveRF.h"
#interface calculatorTableViewController ()
#end
#implementation calculatorTableViewController{
NSArray *general;
NSArray *digital;
NSArray *print;
NSArray *television;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
general = [NSArray arrayWithObjects:#"Calculate GRPs (Have R & F)", #"Calculate Reach (Have GRPs & F)", #"Calculate Frequency (Have GRPs & R)", #"CPM to CPP", #"CPP to CPM", nil];
digital = [NSArray arrayWithObjects:#"Calculate Impressions (Have cost & CPM)", #"Calculate CPM (Have cost & impressions)", #"Calculate Cost (Have CPM and Impressions)", #"Calculate ASF", #"Calculate CPC/CPA/CPV", nil];
print = [NSArray arrayWithObjects:#"Calculate CPM", #"Calculate GRPs", #"Calculate CPP", nil];
television = [NSArray arrayWithObjects:#"Universe Estimates", #"Calculate GRPs", #"Calculate Spots", #"Calculate Average Rating", #"Calculate Total Impressions", #"Calculate CPP", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([_calculationType isEqualToString:#"General"]){
return [general count];
}
else if ([_calculationType isEqualToString:#"Digital"]){
return [digital count];
}
else if ([_calculationType isEqualToString:#"Print"]){
return [print count];
}
else if ([_calculationType isEqualToString:#"Television"]){
return [television count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Calculator2Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if ([_calculationType isEqualToString:#"General"]) {
cell.textLabel.text = [general objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
else if ([_calculationType isEqualToString:#"Digital"]) {
cell.textLabel.text = [digital objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
else if ([_calculationType isEqualToString:#"Print"]) {
cell.textLabel.text = [print objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
else if ([_calculationType isEqualToString:#"Television"]) {
cell.textLabel.text = [television objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12];
}
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Calculate GRPs (Have R & F)"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
CalculateGrpsHaveRF *destViewController = segue.destinationViewController;
destViewController.Title = [general objectAtIndex:indexPath.row];
#end
If I understand correctly, you want calculatorTableViewController to perform segue to next view.
//when you select a row
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//perform a segue
if (indexPath.row == 1)
{
[self performSegueWithIdentifier:#"your_segue_identifier" sender:your_sender];
}
else if (indexPath.row == 2)
{
[self performSegueWithIdentifier:#"another_segue_identifier" sender:your_sender];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//prepare your segue here
}
I'm using this tutorial on how to create a pop out menu.
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
I got it working when going through the tutorial, now i'm trying to implement it into my app.
I'm at the stage where i can press the menu button and the popout menu appears. The problem is, it doesn't populate itself with my table view cells.
I set the identifiers for each table view cell and then in the code reference them to full an array.
I know when woking through the tutorial, if I misspelled one of the identifiers when defining what's in the array, the program would crash. In my app, that's not the case. Hopefully that can help pin down the problem. It doesn't even change the colours which is the first part of the code.
Here's the code.
#import "SidebarViewController.h"
#import "SWRevealViewController.h"
#interface SidebarViewController ()
#property (nonatomic, strong) NSArray *menuItems;
#end
#implementation SidebarViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
_menuItems = #[#"markup",#"tax"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Any help would be great.
Thanks
All you're doing is creating an array with two string literals. You must set the textlabel's text property in the following method to display the strings in 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] autorelease];
}
cell.textLabel.text = [_menuItems objectAtIndexPath:indexPath.row];
return cell;
}
OMG I feel like such a noob. It turns out the ViewControler didn't have a target, hence why it wasn't responding.
I'm so sorry for wasting your time, I really feel bad now.
I have a UITableView and another view. The other view has a UITextView that I want populated with different text depending on what row in the table view is selected. I know how to do it if I create different views for all of the different text options. If I did it that way, I could just have that view open when the row is tapped. I'm just wanting to avoid having to create a bunch of different views and just use one that gets loaded with different text depending on row tapped in the table view. I guess I just need to know how the view with the text view can access indexPath.row from the table view. Any suggestions?
EDIT: Here is my code.
LHRRoster.m
#import "LHRRoster.h"
#import "CustomCellBackground.h"
#import "OnMyHonor.h"
#import "AVA.h"
#import "Receivers.h"
#import "BandDetailView.h"
#interface LHRRoster ()
#end
#implementation LHRRoster
{
NSMutableArray *mbTableData;
}
- (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.
mbTableData = [NSMutableArray arrayWithObjects:#"Aesthetics Versus Architecture", #"On My Honor", #"Receivers", #"Skyscraper Stereo", #"California", #"Late Ones", #"Uh-Huh Baby Yeah", #"Danielle Bouchard", #"Chris Karrer", #"Joey Blue", #"The Codas", #"Your Favorite Hero", #"Nixon", #"Skam Impaired", #" SaySomethingHugeOnFire", #"Adore Me Not", #"Common Collective", #"The Royal Tees", #"The Gravetones", #"Bush League", #"Rock & Roll Television", #" Tastyface", #"The Lips", #"Mercy Academy", #"Audiostrobelight", nil];
self.title = #"LHR Roster";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mbTableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *mbTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mbTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mbTableIdentifier];
cell.textLabel.font=[UIFont systemFontOfSize:14.0];
}
cell.backgroundView = [[CustomCellBackground alloc] init];
cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
cell.textLabel.text = [mbTableData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BandDetailView *detailView = [[BandDetailView alloc]initWithNibName:#"BandDetailView" bundle:nil];
if (indexPath.row == 0)
{
detailView.title = #"Aesthetics vs Architecture";
UIImage *image = [UIImage imageNamed: #"AVA.png"];
[detailView.bandPic setImage:image];
NSString *bio = #"";
[detailView.bandInfo setText:bio];
[self.navigationController pushViewController:detailView animated:YES];
}
}
#end
You are getting the selected cell object, just pass that object to the TextView and it will dynamically load selected cell data.An important thing to note,first push view controller then pass data to that controller.In your code you have passed data first,then pushed view controller, that doesnt work
MVC - model, view, controller. Your data is model. The table view and the text view are view. Your UIViewController is the controller. It does all the work.
Implement didSelectRowAtIndexPath in the controller, which is the delegate of the table view. Now you know that a row of the table was selected, and you know which row it is. So pull the desired text out of your model and stick it in the UITextView.
Expand your didSelectRowAtIndexPath to also modify your UITextView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
**myUiTextView.text = somethingBasedOnTheRowSelected[indexPath.row];** <-- new code here
// followed by your existing code
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BandDetailView *detailView = [[BandDetailView alloc]initWithNibName:#"BandDetailView" bundle:nil];
if (indexPath.row == 0)
{
detailView.title = #"Aesthetics vs Architecture";
UIImage *image = [UIImage imageNamed: #"AVA.png"];
[detailView.bandPic setImage:image];
NSString *bio = #"";
[detailView.bandInfo setText:bio];
[self.navigationController pushViewController:detailView animated:YES];
}
**myUiTextView.text =[yourarray indexPath.row];
}
follow this code this code might help you.
Just Add your code is
yourtextfieldname.text=[yourarrayname objectAtIndex:indexpath.row];
and get your selected row textfield data in your textfield.
i hope this code is useful for you .
I am trying to create a UITableView application on Xcode 4.2. I just want each cells (ex. Cali) to push a new UIViewController when it's touched.
The issue I'm running into is whenever I press the cell it's not pushing the new view controller.
When I put a break point at the didSelectRowAtIndexPath method, I see 5 threads.
Here's my code:
#import <UIKit/UIKit.h>
#interface Adam : UITableViewController
{
NSMutableArray *states;
}
#end
#import "Adam.h"
#import "ViewController.h"
#implementation Adam
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSMutableArray arrayWithObjects:
#"cali",
#"ohio",
nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [states 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];
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
[cellLabel setText:[states objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[states objectAtIndex:indexPath.row] isEqual:#"cali"])
{
ViewController *cali = [[ViewController alloc] initWithNibName:#"cali" bundle:nil];
[self.navigationController pushViewController:cali animated:YES];
}
}
#end
First the TableViewCell has a textLabel property so you don't need to create your own label.
cell.textLabel.text= [states objectAtIndex:indexPath.row];
and is there a reason your deselecting the cell?Try deselecting it after you use the indexPath
And try:
if([states objectAtIndex:indexPath.row] isEqualToString:#"cali"]];){
}