How to populate tableviews from UIViewController - uitableview

I want to add 4 UITableViews on my scrollview and poppulate them with some different arrays(let's say i have one array for each). I have added this scrollview too to my self.view(i do this stuff on a UIViewController class). How can i populate my Tableviews can anyone help please?
Some More Details:
here is a screen shot;
Here is the interface of my UIViewControllerClass
#import <UIKit/UIKit.h>
#interface MainMenu : UIViewController<UITableViewDelegate>
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#property (retain, nonatomic) IBOutlet UITableView *group1;
#property (retain, nonatomic) IBOutlet UITableView *group2;
#property (retain, nonatomic) IBOutlet UITableView *group3;
#property (retain, nonatomic) IBOutlet UITableView *group4;
#end//i drag/dropped from IB, so there is no problem with synthesizes..
I want to populate these tableview with different arrays, how to handle this situation..? Thanks a lot
Additional Note; i tried something like this but no effect:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [group1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *array=[NSArray arrayWithObjects:#"one",#"two",#"tree", nil];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}

dude, first you will make the uiviewcontroller de delegate and data source of all 4 tables, after that you will assign a different tag for each table.. and then you will add the delegate and datasource protocols... and the you will implement the datasource and delegate methods like a normal uitableviewcontroller... all these methods receive a Uitableview parameter, so you will test the tag of this table view like:
...
if (tableView.tag == 10) {
//implement the table 1
NSArray *array=[NSArray arrayWithObjects:#"one",#"two",#"tree", nil];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
} else if (tableView.tag == 20) {
//implement the table 2
} else if (tableView.tag == 30) {
//implement the table 3
} else if (tableView.tag == 40) {
//implement the table 4
}
return cell;
this way you will use the same method to do all the table views..

Related

Reuse custom TableViewCell

I have a viewController that contains a programmatically created tableView.
#property (strong, nonatomic) UITableView *numbersTableView;
//...
self.numbersTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width-20, 50)];
self.numbersTableView.delegate = self;
self.numbersTableView.dataSource = self;
self.numbersTableView.tag = 1;
self.numbersTableView.layer.cornerRadius = 5;
[self.numbersTableView setBounces:false];
[self.numbersTableView setHidden:true];
[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:#"NumbersTableViewCell"];
[self.view addSubview:self.numbersTableView];
For the prototype cell I want to use a prototype that I created somewhere else in another viewController and I designed It in storyboard.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.tag == 1) { //tableView == self.numbersTableView
NSString *cellID = #"NumbersTableViewCell";
AZGCountryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[AZGCountryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.countryName.text = self.numbersArray[indexPath.row].number;
cell.flagImageView.image = [UIImage imageNamed:self.numbersArray[indexPath.row].country];
return cell;
}
}
And my custom cell contains one UIImageView and one UILabel but when I run the code the cells are empty and I can see the UIImageView and UILabel are nil!
#import <UIKit/UIKit.h>
#interface AZGCountryCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *flagImageView;
#property (strong, nonatomic) IBOutlet UILabel *countryName;
#property (strong, nonatomic) IBOutlet UILabel *countryCode;
#end
#import "AZGCountryCell.h"
#implementation AZGCountryCell
#synthesize flagImageView;
#synthesize countryName;
- (void)awakeFromNib {
[super awakeFromNib];
self.flagImageView.layer.cornerRadius = self.flagImageView.frame.size.width/2;
self.flagImageView.layer.masksToBounds = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
Then what should I do to have properly filled cells in my numbersTableView?
Unfortunately you can't reuse a table cell you've defined in a table view in a storyboard in your separate class.
In order for you to be able to share your cell between the different views, you'll have to extract the view to a separate nib and register that nib go be used on both tables.
Alternatively you can implement the cell's UI in code in the table view cell subclass.
You getting empty cell because custom tableview cell not registered, use
[self.numbersTableView registerNib:[UINib nibWithNibName:#"AZGCountryCell" bundle:nil] forCellReuseIdentifier:#"NumbersTableViewCell"];
instead of
[self.numbersTableView registerClass:[AZGCountryCell class] forCellReuseIdentifier:#"NumbersTableViewCell"];

Need help setting up custom cell

I've never implemented a custom cell for a tableView before and am confused on how to proceed. I'm trying to make a personality test app where each question is represented in a cell of my tableView and underneath each question are several buttons that will act as a response to the corresponding question. I've managed to set up my cell with a textview for the question and several buttons with appropriate constraints so they won't move.
I also set the Cell style to custom in the attributes inspector.
Can someone help me with how to set up the textview with my question? I tried making a property of UITextView and linking it to TextView in the cell but then an error arose saying I wasn't allowed to populate a reoccurring cell with an IBOutlet.
Here is a screenshot of my storyboard. Let me know if you need to see anything else.
TestVC.h
#import <UIKit/UIKit.h>
#import "Questions.h"
#import "CustomCell.h"
#interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) IBOutlet UITextView *instructions;
#property (weak, nonatomic) IBOutlet UIButton *results;
//#property (strong, nonatomic) IBOutlet *question;
#property (nonatomic, strong) Questions *survey;
-(IBAction)backPressed:(id)sender;
#end
TestViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [survey.questions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
/*
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
*/
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
//UITextView *textView = (UITextView *)[cell viewWithTag: 100];
cell.textView = (UITextView *)[cell viewWithTag: 100];
cell.textView.text = que;
return cell;
}
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
// Variables for questions and answers
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (weak, nonatomic) IBOutlet UIButton *strongAgree;
#property (weak, nonatomic) IBOutlet UIButton *agree;
#property (weak, nonatomic) IBOutlet UIButton *weakAgree;
#property (weak, nonatomic) IBOutlet UIButton *neutral;
#property (weak, nonatomic) IBOutlet UIButton *strongDisagree;
#property (weak, nonatomic) IBOutlet UIButton *disagree;
#property (weak, nonatomic) IBOutlet UIButton *weakDisagree;
#end
CustomCell.m
#import "CustomCell.h"
#interface CustomCell ()
#end
#implementation CustomCell
#synthesize textView;
#synthesize strongAgree;
#synthesize agree;
#synthesize weakAgree;
#synthesize neutral;
#synthesize strongDisagree;
#synthesize disagree;
#synthesize weakDisagree;
#end
Set a tag for the UITextView in the attributes inspector and get it by the viewWithTag Method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
UITextView *textView = (UITextView *)[cell viewWithTag:100];//Use your tag
textView.text = que;
return cell;
}
this should work for you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
cell.textLabel.text = que;
return cell;
}
From what I see here, you doesn't create a custom cell, just a normal UITableViewCell
You need to create a subclass of UITableViewCell. Call it CustomCell or whatever, then give it the IBoutlet properties that you created in your storybard.
For exemple, in CustomCell.h :
#property (weak, nonatomic) IBOutlet UITextView textView;
Then in the cellForRowAtIndexPath :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// EDIT
if (!cell)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}//
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
cell.textview.text = que;
return cell;
}
Finally in you storyboard, select the cell you customized, and setup its class in the inspector as CustomCell.
NB : Your IBOutlet properties should always be weak properties.

Properties of custom UITableViewCell are nil

I'm trying to use a custom UITableViewCell inside a UITableViewController. I've done the following setup:
Create custom class DPInAppPurchaseCell
#import <UIKit/UIKit.h>
#import "DPBuyButton.h"
#interface DPInAppPurchaseCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
#property (strong, nonatomic) IBOutlet DPBuyButton *buyButton;
#end
Assign custom class in storyboard
Assign cell identifier in storyboard
Register class in viewDidLoad
[self.tableView registerClass:[DPInAppPurchaseCell class] forCellReuseIdentifier:#"InAppPurchaseCell"];
Create cell in UITableViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DPInAppPurchaseCell *cell = (DPInAppPurchaseCell*)[tableView dequeueReusableCellWithIdentifier:#"InAppPurchaseCell" forIndexPath:indexPath];
SKProduct *product = [self.products objectAtIndex:indexPath.row];
cell.buyButton.titleLabel.text = [product.price stringValue];
cell.descriptionLabel.text = product.localizedDescription;
cell.buyButton.tag = indexPath.row;
[cell.buyButton addTarget:self action:#selector(buyPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Properties of cell
The problem is that the properties of the cell are nil and therefore I can't see it in my
UITableViewController. I've also set a breakpoint to awakeFromNib: and initWithCoder:
and both methods of my custom cell aren't called at all.
Edit
Properties are assigned from Storyboard to .h file.

Custom cell not displaying PFImageView?

I have a custom PFTableViewCell using a NIB. All of my custom labels display, but my PFImageView does not. I changed everything to a UIImageView just to make sure it wasn't something wrong with my Parse call, but I couldn't get it to work even with setting a static image in my resources.
My cell's class is set as foodCell and foodcell.h is set as the owner.
Here is a screenshot showing the imageview on my NIB
Here is my foodCell.h to show the outlet of the PFImageView :
#import <Parse/Parse.h>
#interface foodCell : PFTableViewCell
#property (weak, nonatomic) IBOutlet NSObject *foodCell;
#property (weak, nonatomic) IBOutlet UILabel *priceLabel;
#property (weak, nonatomic) IBOutlet UILabel *foodDescription;
#property (weak, nonatomic) IBOutlet UILabel *foodTitle;
#property (strong, nonatomic) IBOutlet PFImageView *foodPic;
#end
Now here is where I configure my cell. Again, all of my other code here works, except the foodCell.foodPic.file :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"foodCell";
foodCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[foodCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.foodPic.file = [object objectForKey:#"foodImage"];
cell.foodTitle.text = [object objectForKey:#"foodName"];
cell.foodDescription.text = [object objectForKey:#"foodDescription"];
NSString *myString = [#"$" stringByAppendingString:[NSString stringWithFormat:#"%1#", [object objectForKey:#"foodPrice"]]];
cell.priceLabel.text = myString;
return cell;
}
As per Parse documentation, you should call loadInBackground for that PFImageView
cell.foodPic.file = [object objectForKey:#"foodImage"];
[cell.foodPic loadInBackground];
Also call [PFImageView class] in your AppDelegate.m didFinishLaunchWithOptions: method.
source: https://parse.com/questions/using-pfimageview-with-storyboard

Custom UITableViewCell doesn't appear in the table

I have created a UITableViewCell and I am trying to display it in one of my table, but for some reason it doesn't come, the table is coming empty. And when I click on the one of cell in the table it takes me down to next level, which means the custom UITableViewCell is not getting rendered properly.
Here is my custom UITableViewCell .h file
#interface ProductViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *detailLabel;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UILabel *detailLabel;
#property (nonatomic, retain) UIImageView *imageView;
Here is the .m file
#import "ProductViewCell.h"
#implementation ProductViewCell
#synthesize titleLabel, detailLabel, imageView;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
Here is the .h file which subclasses UITableViewController
#interface ProductCategoryViewController : UITableViewController {
NSArray *tableDataSource;
}
#property (nonatomic, retain) NSArray *tableDataSource;
Here is the .m file for ProductCategoryViewController
#implementation ProductCategoryViewController
#synthesize tableDataSource;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
ProductViewCell *cell = (ProductViewCell*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[ProductViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dictionary objectForKey:#"name"];
cell.imageView.image = [UIImage imageNamed:[dictionary objectForKey:#"imageUrl"]];
return cell;
}
Your help is appreciated.
Thanks,
Yogesh
I got this working by making some changes, in my tableviewcontroller I added an IBOutlet for ProductViewCell and synthesis the same.
Then I changed the xib file for ProductViewCell
1) I make File's Owner to my tableViewController
2) Then here I linked the the IBOutlet that I created in the tableViewContoller to the ProductViewCell.
I am not sure if this is the right way to do it but atleast this is working for me now

Resources