Hi I have an requirement to customize UITableViewCell. Hence I have created a custom class and necessary UI (xib) to support it. For the XIB I have chosen the class as the derived class that I have created. My Problem is when after linking the display labels to the properties and me setting the values at runtime does not display the text desired. Its left as blank. Below is the code snippet.
#interface CustomCell : UITableViewCell
{
IBOutlet UILabel *titleRow;
}
#property (nonatomic, strong) UILabel *titleRow;
#property (nonatomic, strong) UILabel *subTitleRow;
#property (nonatomic, strong) UILabel *otherTextRow;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MedVaultCell";
CustomCell *cell = nil;
cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (nil == cell){
//Load custom cell from NIB file
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCellHistoryCell" owner:self options:NULL];
cell = [nib objectAtIndex:0];
//cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// get the object
Weight *currentCellWeight = [_weights objectAtIndex:indexPath.row];
// Configure the cell...
UILabel *titleLable = [[UILabel alloc]init];
titleLable.text = currentCellWeight.customDispText;
[cell setTitleRow:titleLable];
cell.titleRow.text = currentCellWeight.display;
cell.titleRow.textColor = [UIColor redColor];
//cell.textLabel.text = [[_weights objectAtIndex:indexPath.row] customDispText];
//cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
First, I hope your cellForRowAtIndexPath is in your UITableView delegate, not in your custom cell class.
Secondly, here is the problem:
// Configure the cell...
UILabel *titleLable = [[UILabel alloc]init];
titleLable.text = currentCellWeight.customDispText;
[cell setTitleRow:titleLable];
In this code you are creating a new label and overriding your IBOutlet label with the new label. Then you are not displaying the new label. Instead, change the code to this:
// Configure the cell...
cell.titleRow.text = currentCellWeight.customDispText;
However, you then reset the titleRow.text right after it to currentCellWeight.display.
So you need to pick which one of those you want to be the text and set the text to that. You do not need to create a new label (UILabel *titleLable = [[UILabel alloc] init];) because you have already created the label in IB.
Related
I have a UITableView with two different custom table cells. The first cell appears normal after I start the app. The second cell will appear when you click on them.
Can anybody help me or has an idea?
Thanks a lot.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"customCell2";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.font = [UIFont fontWithName:#"STHeitiSC-Light" size:9.0];
}
return cell;
}
Having done custom UITableViewCell in the past I usually handle the nib loading in the custom class itself.
The basic header for the custom cell.
#interface RequestsTableViewCell : UITableViewCell {
// Ivars.
}
// Properties.
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType;
// Other methods, etc.
#end
The custom cell with a designated initializer.
#implementation RequestsTableViewCell
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"RequestsTableViewCell" owner:self options:nil];
self = [nibArray objectAtIndex:0];
requestModel = model;
queryType = requestType;
[self setRequestThumbnail];
[self setRequestCategory];
[self setRequestAddress];
[self setRequestStatusDate];
[self setRequestStatus];
[self setRequestFollowed];
[self setRequestComment];
[self setAppearance];
}
return self;
}
There would also be a custom xib for the custom UITableViewCell that corresponds and has the custom class set in the identity inspector.
In the UITableViewController.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell Id";
RequestModel *request = nil;
// Other code for search, etc
request = [self.serviceRequests objectAtIndex:indexPath.row];
RequestsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell) {
cell = [[RequestsTableViewCell alloc] initWithRequestModel:request style:UITableViewCellStyleDefault reuseIdentifier:cellId forQueryType:queryTypeIndicator];
}
return cell;
}
It also sounds like you have more than one custom cell type in your question? Can you elaborate on how it is all supposed to function? You say that you have to click one cell to make another appear, can you explain that interaction?
I did something similar, but made the cell 'expand', instead of adding a new cell. Of course then you don't have two cells, but you can resize your one cell, add subframes,...
You can keep a boolean in your UITableViewCell object (BOOL cellIsExpanded), and set that on tap gesture. Then in drawRect of the TableViewCell, layout your cell accordingly.
Example code, on expand, make cell height 20-->80 and add a UIButton:
In the TableViewController, overload heightForRowAtIndexPath (this will resize your cell if 'expanded'):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
YourEntity *record = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (!record.cellIsExpanded)
return 20.; // cell is smaller if collapsed
else
return 80.; // bigger cell
}
In the TableViewCell, add or remove subframes:
#interface MyTableViewCell ()
#property(nonatomic) BOOL cellIsExpanded
#property(strong, nonatomic) UITextField *myTextField;
#property(strong, nonatomic) UIButton *clickMeButton;
#end
#implementation MyTableViewCell
- (void)drawRect:(CGRect)rect {
if(!self.cellIsExpanded){
// layout your collapsed cell, for example:
self.myTextField = [[UITextField alloc] initWithFrame:self.frame];
self.myTextField.text = #"Collapsed cell";
// remove button, only present in expanded view :
self.clickMeButton=nil;
}
else{
self.myTextField.text = #"Expanded cell";
// add button below textfield
self.clickMeButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 10, 10)];
}
}
#end
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 dynamically tableView with custom cell. CustomCell .h file looks like this:
#property (strong, nonatomic) IBOutlet UILabel *uslugaName; //I set retain doesn't work too
#property (strong, nonatomic) IBOutlet UILabel *howMuchPayLbl;
My CellForRowAtIndexPathMethod:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellIdentifier = #"Cell";
myCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
/*
if (!cell)
cell = [[myCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
*/
if (indexPath.row !=15) {
cell.uslugaName.text =serviceNameArr[indexPath.row];
//окрашиваем ячейку в зависимости от активности услуги
if ([uslugaIsActiveArr[indexPath.row] isEqual: #"1"]) {
cell.backgroundColor = [UIColor blackColor];
cell.howMuchPayLbl.enabled = YES;
}
else {
cell.backgroundColor = [UIColor grayColor];
cell.howMuchPayLbl.enabled = NO;
}
if (![amountTmpArr[indexPath.row] isEqual: #"0"])
cell.howMuchPayLbl.text = [NSString stringWithFormat:#"Оплачиваю: %# KZT", amountTmpArr[indexPath.row]];
}
else {
cell.uslugaName.font = [UIFont fontWithName:#"System Bold" size:16];
cell.uslugaName.text = [NSString stringWithFormat:#"ОБЩАЯ СУММА ОПЛАТЫ: %#", fullAmount];
cell.howMuchPayLbl.hidden = YES;
}
return cell;
}
I want that last row different than others ( for this purpose this:
if (indexPath.row !=15)
). Problem is - when scrolling cell.howMuchPayLb disappear. If delete special code for last row - all works ok, why this happening?
Your code has an if else statement where one branch can set cell.howMuchPayLbl.hidden = YES; but the other branch does not set cell.howMuchPayLbl.hidden = NO;. So, once the label is hidden it will never be un-hidden. When the cell with the hidden label is reused the label remains hidden.
Add cell.howMuchPayLbl.hidden = NO; (and any other 'inverse' configuration required) to your if statement.
Refer This link will help you..
Its because of dequeueReusableCellWithIdentifier will not recognize the cell-identifire with same name.So you can use unique cell identifire like Cell1,Cell2...for each row..
I'm using storyboard for a tableview with custom cells. I created custom cells in storyboard without any additional classes for it. Everything works fine when I'm not using the UISearch bar, But when I want to create the custom cell for UISearchBarController tableview I don't know how can I do it.
Hers is my custom cell in storyboard,
with cellIdentifier = "SelectionCell"
and I'm not using any custom class.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SelectionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
the code crashes:
'Could not load NIB in bundle:'
I don't have a NIB file for my custom cell, and I don't have any custom UITableViewCell class to use alloc/init method like this:
cell = [[myCustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
how can I create the cell?
searchResultsTableView
The table view in which the search results are displayed. (read-only)
#property(nonatomic, readonly) UITableView *searchResultsTableView
A search display controller manages the display of a search bar, along with a table view(your original tableview style) that displays search results.
It seems it can't be done without a custom cell class. Because we don’t have a storyboard prototype cell to manipulate graphically for the filtered table view, we have to programmatically create the cells. And for that we need a custom cell class and with initWithStyle: method in the .m file as follows.
So I created a class called SelectionCell subclass of UITableViewCell, and in IB assigned custom cell class to SelectionCell class and create IBOutlet for all UI controls in my custom cell into SelectionCell.h file
#interface SelectionCell : UITableViewCell{
}
#property (strong, nonatomic) IBOutlet UIImageView *contactPictureID;
#property (strong, nonatomic) IBOutlet UILabel *contactName;
#property (strong, nonatomic) IBOutlet UILabel *emailAddress;
#property (strong, nonatomic) IBOutlet UIImageView *selectedEmailState;
#end
in initWithStyle: method, I had to recreate all the UI controls programmatically and add them to the contentView as subviews:
// SelectionCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
_contactPictureID = [[UIImageView alloc] initWithFrame:(CGRectMake(10, 14, 48, 48))];
_contactName = [[UILabel alloc] initWithFrame:(CGRectMake(66, 14, 201, 30))];
_contactName.font = [UIFont boldSystemFontOfSize:20];
_contactName.textAlignment = NSTextAlignmentLeft;
_emailAddress = [[UILabel alloc] initWithFrame:(CGRectMake(66, 41, 201, 21))];
_emailAddress.font = [UIFont systemFontOfSize:16];
_emailAddress.textAlignment = NSTextAlignmentLeft;
_selectedEmailState = [[UIImageView alloc] initWithFrame:(CGRectMake(275, 22, 32, 32))];
[self.contentView addSubview:_contactPictureID];
[self.contentView addSubview:_contactName];
[self.contentView addSubview:_emailAddress];
[self.contentView addSubview:_selectedEmailState];
}
return self;
}
Now I am able to instantiate my custom cell. In cellForRowAtIndexPath method
static NSString *CellIdentifier = #"SelectionCell";
SelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SelectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
And now everything is working as it should be.
Where Do i add my custom tableview cell label in my UITableview when using NSCoder?
i already Created The UITableViewCell class and hooked everything up in interface builder.
I tried to replace cell.textLabel.text = oneName.name; with cell.label.text = oneName.name; and it just shows a black square.
- (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];
}
// Configure the cell...
NSUInteger row = [indexPath row];
DrillObject *oneName = [self.addDrill objectAtIndex:row];
cell.textLabel.text = oneName.name;
return cell;
}
Are you sure your label is of the right size? When using custom cells, I really recommend using the free Sensible TableView framework. The framework automatically loads your objects' data into the custom labels, and will also automatically resize the labels/cells whenever needed.
There are tow options to add label to your CustomCell class like you want here:
1- add a label onto your cell in xib file and the assign a tag for it and then create a getter like this:
-(UILabel*)label
{
id label = [self viewByTag:3];
if([label isKindOfClass:[UILabel class]])
{
return label;
}
return nil;
}
2- create the label in the init method and DONT forget to set the label background color to clear color like this:
UILabel *label = [UILabel alloc] initWithFrame:myFreame];
[label setBackgroundColor:[UIColor clearColor]];
// now you should have property on .h file like this
#property (nonatomic, weak) UILabel *label;
// so back to the init dont forget to do this
_label = label;
Thats it.