Displaying different content view in the same uiviewcontroller by uitableview indexpath row - objective-c

I want to show different UIImageView in the same UIViewController depends on which indexpath row was selected in UITableView. I have an unwise approach which was created lots of UIViewController for different UIImageView, But i don't want do this. Is there any smarter approach can fulfilled that. You help is very appreciated.

First add an UIImageView to the UIViewController's view.
Create a NSMutableArray to store UIImageViews, and in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
imageView = [imageViewArray objectAtIndex:indexPath.row];
}

Related

Custom Table Cell View doesn't get modified when load table data

I have a little problem with a custom NSTableCellView.
The problem: I have a Table View in a Nib with all stuff configured as well to load data from an array. For now, is OK. The problem is, while the app is preparing the views (using method (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row), the returned View is modified as I want (watched by a breakpoint before return), but the View that I can see, that everybody can see, is not modified.
I expect you could help me found the problem. I give you some stuff to help to understand my problem.
Some Images
Cell before get returned, as you can see, the View is modified as well.
Cell displayed in the Table View, in this case, the Custom View is not modified, it's look likes I didn't modified anything
Code
My custom NSTableCellView, there's only a header...
#interface playlistQueueItemView : NSTableCellView
#property (weak) IBOutlet NSButton *button;
#property (strong) IBOutlet NSTextField *title;
#property (strong) IBOutlet NSTextField *artist;
- (void)setAllFromInitWithTitle:(NSString*)title artist:(NSString*)artist;
#end
Method for create the Views for the table:
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
playlistQueueItemView *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
NSDictionary* song = [[PlaylistQueue getQueue] getSongWithPos:(int) row];
cell.artist.stringValue = song[#"artista"];
cell.title.stringValue = song[#"titulo"];
return cell;
}
One more thing, I followed a bit one of the Apple's sample projects, called TableViewPlayground.
Thanks :D
you have to override - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row and then return the view.
In - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row you can return a model object that has the value you want to populate.
source

Error adding UITableView to ViewController on storyboard

On my storyboard I have a ViewController and I want to add a UITableView with 1 section and 2 static cells. That's all I'm doing right now but I get this error when compiling.
Static table views are only valid when embedded in UITableViewController instances
What am I missing?
Look at This question. Basically the same thing.
Any further questions, just ask! :)
Edited stuff:
UITableViewController *controller = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.view addSubview:controller.tableView];
Then all you need to do is adding the UITableViewDataSource in the header file and you need the datasource methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([indexPath row] = 0){
do something
}else if([indexPath row] = 1){
do something else
}
I think you just have to set datasource and delegate methods of the tableView in your .h file
#interface viewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
and then link it to your tableView in storyboard.
Please notify if it works..

Is it possible to create a static tableview in a storyboard and add it programmatically later? (iOS5, XCode 4)

I'm trying to create a couple of small, static tableviews and add them to a panel that I have which slides in and out. The panel is created programmatically so I can't lay the tableviews out inside it via storyboard, and anyway I'm not sure if this is possible anyhow: It seems the only way you can lay out static tableviews that work is in a tableviewcontroller, which takes up the whole screen.
If you can't tell I'm pretty new to iOS dev so if I'm not understanding some fundamental concepts here please feel free to explain.
Of course is possible. Here is how it can be done:
Drag a TableViewController to your storyboard.
Set its Size to Freeform, add an identifier and uncheck Resize View From NIB
Select the tableview and set its content to Static Cells. Design your cells.
Set its size
Now, wherever you need to instantiate it do it like this:
// I am using a UITableViewController as an example here
// you probably would like to set your actual controller subclass instead
UITableViewController *tableViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"staticTv"];
UITableView *tableView = tableViewController.tableView;
[self.view addSubview:tableView]; // Or add it to whatever view
Enjoy :)
A UITableViewController isn't necessary to provide the functionality you need to manage a UITableView. I think what you're looking for is the "Delegate" pattern. Any UIViewController can be assigned to be the delegate of the UITableView. For example, I have a "static" table that shows some options in an app I am working on:
#interface LBOptionsViewController : UIViewController <UITableViewDataSource,
UITableViewDelegate>
If you're creating your table views programmatically, you'll probably either be creating them in viewDidLoad or loadView (if you're creating the actual view yourself). After you've created your tableView, assign the delegates:
self.tableView.delegate = self;
self.tableView.dataSource = self;
Then your UIViewController subclass will receive the data delegate messages like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Not sure if this helps you. I have not played with Storyboards much yet.
EDIT: #Alladinian has the right answer! If you're using an property for the view controller make sure you allocate it if you need it to be called by other methods.
I've yet to find a usefully reason to use static table view cells over dynamic. Table views were pretty scary when I started iOS programming. I used sqlite in my first app YIKES.
So yeah, you should just import the UITableView Data Source and Delegate and follow up by adding the table view to your panel (assuming it's a uiview and you can add the table view as a subview).
Anyways in your ViewController.h include UITableViewDataSource and UITableViewDelegate.
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
Next, add properties for a UITableView and an NSMutableArray:
#property (strong, nonatomic) UITableView* tableView;
#property (strong, nonatomic) NSMutableArray* tableViewContents;
In your ViewController's .m:
#synthesize tableView;
#synthesize tableViewContents;
inside ViewDidLoad:
self.tableViewContents = [NSMutableArray arrayWithObjects:#"Cell 1",#"Cell 2",#"Cell 3",nil];
[self.tableView setDelegate:self]
[self.tableView setDatasource:self]
In the .m file:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.tableViewContents.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
index = row;
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [tableViewContents objectAtIndex:row];
return cell;
}

UITableView didSelectRowAtIndexPath method not firing

I placed a TableView on my xib file with the following method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"FIRED THE EVENT");
}
and it doesn't seem to be firing when I click on a cell in the table. Is there something that I'm missing?
Be sure to include UITableViewDataSource, UITableViewDelegate in your .h file

UITableView with multiple datasource

I have a ViewController that needs to use 2 UITableViews.
1 is always showing, while the other will show up as a popup after you click on a button on the view.
Typically I set the delegate and datasource to the File's Owner. However, since 1 of the UITableViews is in a popup, I'm not sure how to best tackle this.
e.g how do I tackle this part -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Please advice.
You should have instance variables for both table views declared in your controller:
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *mainTableView;
UITableView *popupTableView;
}
In each data source or delegate method, you can check which table view is being passed by the caller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == mainTableView)
{
// Code to create and return a main table view cell
}
else if(tableView == popupTableView)
{
// Code to create and return a popup table view cell
}
}