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.
Related
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"];
I have UITableView with custom UITableViewCell. Custom cell contains UIButton and UILabel.
Here I observe that UILabel text is change as I expected but UIButton text is not changing.
When I scroll out the button outside of the screen, change the label of UIButton.
Why it is not working? I have use Xcode 6 and use below code.
ViewController.h
#import <UIKit/UIKit.h>
#import "customTableViewCell.h"
#interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerNib:[UINib nibWithNibName:#"customTableViewCell" bundle:nil] forCellReuseIdentifier:#"customTableViewCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"customTableViewCell" forIndexPath:indexPath];
cell.button.titleLabel.text = #"Label does not change immediately";
cell.label.text = #"change label";
return cell;
}
customeTableViewCell.h
#import <UIKit/UIKit.h>
#interface customTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (weak, nonatomic) IBOutlet UILabel *label;
#end
This is because the UIButton class has multiple states (Normal, Selected, Highlighted, Disabled). When you change the text property of it's internal UILabel (textLabel property of UIButton), it's property is overriden by setState function, which is called when table is loaded.
To change text in the label of a button you need to call setTitle:forState: method. Here is your code fixed to work:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"customTableViewCell" forIndexPath:indexPath];
[cell.button setTitle:#"Now it works" forState:UIControlStateNormal];
cell.label.text = #"change label";
return cell;
}
For the sake of completion, you can also use setAttributedTitle:forState: method with NSAttributedString, so you can actually set your own specifically formatted string as a title.
Use this
[cell.button setTitle:#"MyNewTitle" forState:UIControlStateNormal];
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
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..
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