I made a subclass of UITableViewCell to get a bigger TableViewCell with some more options.
But my problem is that I can't set the text(s) of the label(s):
BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]];
NSLog(#"%#", [bi title]);
[[cell mainLabel] setText:[bi title]];
NSLog(#"%#", [[cell mainLabel] text]);
The first log message returns the text I expected, but the second one always logs (null).
I really don't know what should be wrong. I've created the labels as usual:
#property (weak, nonatomic) IBOutlet UILabel *mainLabel;
Of course I connected the labels and synthesized them (checked it twice). I've also implemented the method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
to get the appropriate height for each cell (which works fine).
BTW, checkmarks appear as expected. It's just about the labels.
Make sure you are instantiating your custom UITableViewCell subclass in the cellForRowAtIndexPath method. Also make sure that those IBOutlets are declared in your UITableView subclass and NOT in the View Controller that houses the TableView. Also make sure that the parent class of your cell in interface builder is set to that same subclass.
Something like this (Custom UITableViewCell subclass interface file):
#import <UIKit/UIKit.h>
#interface MyCustomCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *mainLabel;
#end
Then #synthesize in the implementation file:
#synthesize mainLabel;
Then in cellForRowAtIndexPath, something like:
static NSString *CellIdentifier = #"MyCellIdentifier";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]];
// Configure the cell...
cell.mainLabel.text = [bi title];
// ... Other stuff
return cell;
Related
I am trying to query images in my UITableview, but I am having a problem trying to configure the cell.
Create a subclass of UITableViewCell and add a UIImageView property to it. Here's an example of what your header file would look like:
#import <UIKit/UIKit.h>
#interface CustomTableViewCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIImageView *icon;
//#property (nonatomic, strong) UIImageView *icon; //Only if you're not using an XIB/storyboard
#end
Then, your cellForRowAtIndexPath: function would look something similar to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"cellimage";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//the following assumes that your self.images array contains UIImages
cell.icon.image = [self.images objectAtIndex:indexPath.row];
return cell;
}
Also, be sure to call reloadData on the main thread.
I created a custom table cell that expands. When it expands, it shows a UILabel along with two UIButtons. The UILabel should show data from the backend. I created the customLabel in the CustomCell interface file. Then I imported the CustomCell class into my InboxTableViewController, and implemented the custom cell. The label only appears when the cell is tapped, and expands. How would I go about changing the UILabel, because cell.customLabel.text, would not work here, since it is not a property of the cell. Here is the associated code:
CustomCell.h
#interface CustomCell : UITableViewCell
{
UIImage *userImage;
UILabel *userName;
//below widgets will display when user tapped
#public
UIButton *leftButton, *rightButton;
UILabel *customLabel;
BOOL userTapped;
}
#property (weak, nonatomic) IBOutlet UILabel *userName;
#property (weak, nonatomic) IBOutlet UIImageView *userImage;
- (void) setData:(NSDictionary*) d;
#end
and here is the inboxviewcontroller.m where i try to implement the customcell along with the customLabel.
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// PFObject *message = [self.messages objectAtIndex:indexPath.row];
PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.userName.text = [message objectForKey:#"from"];
//cell.userImage.image = [message objectForKey:#"image1"];
cell.customLabel.text = [message objectForKey:#"message"];
return cell;}
This will not work as you are creating instance of CustomCell directly instead you have to load CustomCell view from nib so it will create instance of customLabel and using this you can set text, use below code
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
For custom UITableViewCell's you should take a look at this tutorial . It will give you the idea.
I have noticed that UITableViewCell is not reusing it's subviews and seems to be released when scrolling up and down. I am creating UITableViewCell subview and animating it on viewDidAppear. I want to do this only once and then I expect subview to stay in the ContentView forever until view disappears. So I have done following which I think right way but not sure what's wrong; can someone help?
UINib *nib1 = [UINib nibWithNibName:#"CustomCell" bundle:nil];
[self.tableView registerNib:nib1 forCellReuseIdentifier:#"CustomCell"];
#interface CustomCell : UITableViewCell
#property (nonatomic) IBOutlet UILabel *title;
#property (nonatomic) CustomeView *customeView;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString cellIdentifier = #"CustomCell";
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:accountCellIdentifier forIndexPath:indexPath];
......
customCell.customeView = (CustomeView*)[customCell.contentView viewWithTag:101];
if(!customCell.customeView) {
customCell.customeView = [CustomeView new];
//configure customview...
customCell.customeView.tag = 101;
[customCell.contentView addSubview:customCell.customeView];
}
return cell;
}
Try to change your property type to strong:
#property (strong, nonatomic) CustomeView *customeView;
I have created a subclass of UITableViewCell and I called it DCCustomCell. This is the DCCustomCell.h file.
#import <UIKit/UIKit.h>
#interface DCCustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UILabel *date;
#property (weak, nonatomic) IBOutlet UILabel *price;
#end
All the proprieties are correcty connected with elements in my iPhone.storyboard file.
I also selected the tableViewCell in iPhone.storyboard and i setted my cell's identifier to "CustomCell" and the class to DCCustomCell.
This is the UITableViewController's viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:#"CustomCell"];
// Do any additional setup after loading the view, typically from a nib.
}
and this is the tableView:cellForRowAtIndexPath: method:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"CustomCell";
DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
NSLog(#"%#" ,cell);
cell.imageView.image = [UIImage imageNamed:#"info_button.png"];
cell.title.text = #"Hello!";
cell.date.text = #"21/12/2013";
cell.price.text = #"€15.00";
return cell;
}
The problem is that the imageView is correctly setted, but all the labels are blank. If I change the imageView property name to, for example, eventImageView, the image will not be setted.
This is the result:
I can't figure out how I can solve this problem.
EDIT: if I remove
[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:#"CustomCell"];
from viewDidLoad all seems to work. why?
As you noticed it works when you remove
[self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:#"CustomCell"];
You only have to do this if you use dequeueReusableCellWithIdentifier:indexPath: AND have not already set the custom class in the identity inspector for that cell. If you use registerClass: forCellReuseIdentifier the cell will be initialized with initWithStyle:reuseIdentifier: instead of initWithCoder: thus screwing up your connections you made in the storyboard.
Have you bind the Controls to IBOutlet ?
You can try setting DCCustomCell to UITableviewCell's class directly in Storyboard.
And than you need to set outlets with controls in cell
Try:
if (cell == null) {
//create and initialize cell
}
after
DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
You first check your outlets properly that all the labels are connected & write this also :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"CustomCell";
DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DCCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSLog(#"%#" ,cell);
cell.imageView.image = [UIImage imageNamed:#"info_button.png"];
cell.title.text = #"Hello!";
cell.date.text = #"21/12/2013";
cell.price.text = #"€15.00";
return cell;
}
I am having a problem with custom cell reappearing. I tried to emulate the problem with this example. I have a simple uitableview with 20 rows, my custom cell has a label and UITextfield, the custom cell design with IBOutlets from storyboard. If I type something in textfield at row0, When I scroll, I see same text in textfield at row 14 which is the first row that appears when I scroll. The problem I am trying to solve is more dynamic and I need to load different UIControls dynamically, but I would like to start with this simple example.
here is the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"TextFieldCellIdentifier";
TextFieldCell *cell = (TextFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
if(!cell)
cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
else {
for (UIView* tempView in cell.contentView.subviews) {
if(tempView.tag == 1)
[tempView removeFromSuperview];
}
}
cell.qTextLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
return cell;
}
My custom Cell:
#interface TextFieldCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *qTextLabel;
#property (weak, nonatomic) IBOutlet UITextField *textField;
#end
You don't set the value of textfield text at reused/created cell, obviously the initial value is going to be used which is the text you entered before in case of reused cell or empty text in case of created cell.