Collapsable Cell - ios

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

Related

tableView:indexPathForCell returns nil when clicking in cell

I know indexPath will return nil when the cell is hidden, but my question is why it is not visible?
My case is that:
There is a button in the cell when button is clicked, and it tries to get the indexPath( by selector indexPathForCell: )
Meanwhile the tableview is refreshed by another thread.
Sometimes the indexPath will get nil, my question is because the button is in the cell, when event was fired the cell must exist ( otherwise how should the button be clicked ), what happened causing the cell hidden?
thx.
//Cell.m
#property (nonatomic, weak) UIButton *button;
#property (nonatomic, weak) XXXDelegate *delegate;
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
UIButton * button = xxx;
[self.contentView addSubview:button];
[button addTarget:self action:#selector(xxxClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)xxxClick:(id)sender{
[self.delegate xxxClick:self];
}
//Controller.m
-(void)loadView{
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.delegate = self;
[self.tableView registerClass:[Cell class] forCellReuseIdentifier:#"xxx"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"xxx" forIndexPath:indexPath];
//update cell
cell.delegate = self;
return cell;
}
- (void)xxxClick:(UITableViewCell*) cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//indexPath is nil sometimes? why?
}
And also, the tableview reloads sometimes
Thx guys,I finally got the answer.
When the button is triggered and the tableview is "reloadData" in the same runloop, "reloadData" cause the cell to be invisible, and "indexPathForCell" will get nil.
It is dangerous if you do not check the value of indexPath.
Might you can have wrong reuse identifier which is defined in the custom cell in the storyboard.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : SingleLabelTableCell = tableView.dequeueReusableCellWithIdentifier("SingleLabelTableCell", forIndexPath: indexPath) as! SingleLabelTableCell
let tempDict : NSDictionary = dataArray.objectAtIndex(indexPath.row) as! NSDictionary
cell.nameLabel.text = tempDict.valueForKey("title") as? String
return cell
}
or You can check it on this video tutorial link also
https://www.youtube.com/watch?v=OHue6nFUBO8
Try to use in below format
Cell.h
//
// Cell.h
// Projects
#import <UIKit/UIKit.h>
#interface Cell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIButton *btnCall;
#end
Cell.m
//
// Cell.m
#import "Cell.h"
#implementation NearByDealDetailCell
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
// ControllerVC.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"Cell";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.btnCall.tag = indexPath.row;
[cell.btnCall addTarget:self action:#selector(actionBtnCall:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)actionBtnCall:(id)sender {
UIButton *btn = (UIButton *)sender;
NearByDealModel *activity = (NearByDealModel *)[self.activityList objectAtIndex:[btn tag]];
}

UITextFields text in custom UITableViewCell appears in other cell while Scrolling

I am new to iOS development. i create my app in Xcode 6(Storyboard).
My problem is text enter into textfield, scroll the table view that shuffle the textfield text to other text field randomly. There are two question i saw in stack-overflow like this. but that answer are not solve question.
MoneyEntry.xib is a uitableviewcell with one label and one text box. And add class file to it and connect IBOutlet and add identifier to the uitableviewcell as "MoneyEntryIdentifier".
//MoneyEntryTableViewCell.h
#interface MoneyEntryTableViewCell : UITableViewCell
//Money Entry Cell
#property (strong, nonatomic) IBOutlet UILabel *lblMemName;
#property (strong, nonatomic) IBOutlet UITextField *textAmount;
#end
//MoneyEntryTableViewCell.m
#import "MoneyEntryTableViewCell.h"
#implementation MoneyEntryTableViewCell
#synthesize lblMemName,textAmount;
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
In Main.storyboard add UITableView and connect IBOutlet and add Datasource and delegate.
//MoneyDetailViewController.h
#import <UIKit/UIKit.h>
#interface MoneyDetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITableView *tblVwMoneyEntry;
#end
//MoneyDetailViewController.m
#interface MoneyDetailViewController ()
{
NSArray *tabledata;
}
#end
#implementation MoneyDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
tabledata = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto",#"Full Breakfast", #"Hamburger", #"Ham and Egg Sandwich", #"Creme Brelee",#"White Chocolate Donut", #"Starbucks Coffee", #"Vegetable Curry", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MoneyEntryIdentifier";
static NSString *CellNib = #"MoneyEntry";
MoneyEntryTableViewCell *cell = (MoneyEntryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (MoneyEntryTableViewCell *)[nib objectAtIndex:0];
}
UILabel *lblname = (UILabel *) [cell lblMemName];
UITextField *txtfield = (UITextField *)[cell textAmount];
txtfield.tag = indexPath.row;
lblname.text = [tabledata objectAtIndex:indexPath.row];
txtfield.placeholder =#"0.00";
cell.selectionStyle =UITableViewCellSelectionStyleNone;
return cell;
}
#end
Please explain me detail. Thanks in advance
I found answer.... Just get the textfield text into Dictionary setObject by label text and again check by label Text assign the text to corresponding Textfield.. here is my code...
//In Interface
NSMutableDictionary *amounts;
amounts =[[NSMutableDictionary alloc]init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MoneyEntryIdentifier";
static NSString *CellNib = #"MoneyEntry";
MoneyEntryTableViewCell *cell = (MoneyEntryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (MoneyEntryTableViewCell *)[nib objectAtIndex:0];
}
UILabel *lblname = (UILabel *) [cell lblMemName];
lblname.tag =100;
UITextField *txtfield = (UITextField *)[cell textAmount];
txtfield.tag =indexPath.row;
[txtfield addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
lblname.text = tabledata[indexPath.row];
txtfield.placeholder = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
if ([amounts valueForKey:lblname.text] != nil) {
txtfield.text = [amounts valueForKey:lblname.text];
} else {
txtfield.text = #"";
}
cell.selectionStyle =UITableViewCellSelectionStyleNone;
return cell;
}
-(void)textFieldDidChange:(UITextField *)txtField
{
UILabel *label = (UILabel *)[txtField.superview viewWithTag:100];
NSString *labelString = label.text;
NSString *textFieldString = txtField.text;
[amounts setObject:textFieldString forKey:labelString];
}
There is no error while scroll table view...
Are you doing something to store the text entered into the textfield. The UITableView works by reusing UITableViewCells that are not visible or gone out of the screen to save memory.
So if you enter some text into a textfield in the cell and the cell goes out of the screen, the cell gets queued up for later use. As you scroll new rows are added from this queue, which bring the textfield with your previous text back. This is why you see your text randomly popping everywhere.
You need to save the data from each textfield in a separate array and reset it in cellForRowAtIndexPath: function.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Your code..
Item *item = itemArray[indexPath.row]
cell.txtfield.text = item.amount
}
You can use the UITextField's UIControlEventEditingChanged event to store the changed array values.
-(void)textFieldDidChange:(UITextField *)txtField
{
Item *item = itemArray[txtField.tag]
item.amount = txtField.text
}
You add the textField change method like this
[textField addTarget:self
action:#selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
Use a custom class called Item to store your data.
class Item
{
#property (nonatomic,strong) NSString *itemName;
#property (nonatomic) float amount;
}
itemArray will contain your items.
Item *it1 = [[Item alloc]init]
it1.itemName = #"Eggs Benedict"
it1.amount = ""
....
itemArray = [NSArray arrayWithObjects:it1,it2,it3,nil];

Centering Image In UITableView

I am having a lot of problems centering an image in my UITableViewCell. I have spent countless hours trying all the solutions listed on stack overflow as well as countless other websites. My text will center, but for some reason my image won't center and there is a white line on the left side of the table that I can not get rid of either.(From what I have read in the iOS documentation, this is new in iOS 7, but people have gotten it to go away) I have set all the insets to zero both programmatically and via the inspector. I am using core data to retrieve the images, which is working fine, it is just my image won't center.
Here is my method for the tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]];
UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:#"image"]];
cell.imageView.image = cellImage;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
Along with that, I can't seem to get my label to go above the image, which is a whole other problem. But what I have done to try to fix this is make a custom cell subclass, which didn't work. I am all out of resources and ideas to fix this.
EDIT ABOUT CUSTOM SUBCLASS
So I might have the wrong idea of a custom subclass of UITableViewCell. But what I did was create a class, with two variables, one for the UIImageView and the other for the label, in a class called customCell. Then in the main table view I called them like this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
//cell.textLabel.text = [NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]];
//UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:#"image"]];
//cell.imageView.image = cellImage;
//cell.textLabel.textAlignment = NSTextAlignmentCenter;
//cell.textLabel.textColor = [UIColor whiteColor];
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[customCell.cellTitle setText:[NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]]];
[customCell.cellImage setImage:[UIImage imageWithData:[moment valueForKey:#"image"]]];
return cell;
}
Then simply in the custom cell class I just connected the label and image to an IBOutlet and synthesized them.
EDIT 2 CUSTOM SUBCLASS ADDITION
The header file has two properties. One for cellTitle which is a UILabel and one for cellImage which is a UIImageView.
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *cellImage;
#property (weak, nonatomic) IBOutlet UILabel *cellTitle;
The implementation file:
Here I am just synthesizing the two objects.
#synthesize cellImage;
#synthesize cellTitle;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
CustomCell *customCell = (CustomCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
[customCell.cellTitle setText:[NSString stringWithFormat:#"%#",
[moment valueForKey:#"name"]]];
[customCell.cellImage setImage:
[UIImage imageWithData:[moment valueForKey:#"image"]]];
return cell;
I believe you're returning the default cell, not the customCell
To your cell subclass make a method
- (void)layoutSubviews {
self.cellTitle.frame = CGRectMake(...); //self.view.frame will return the cell's frame
self.cellImage.frame = CGRectMake(...); //you should set the frames of title and imageView however you want them in here
}
(void)layoutSubviews {
[Self.cellImage setCentre:Self.contentView.Centre];}
After a long day of trying to figure this out, I had one little mistake that caused this whole debacle. I had my cell Identifier set to "CellIdentifier" in the inspector. I changed it to "Cell" and edited some code and boom!
I added some code to the CustomCell:
-(void)layoutSubviews {
self.imageView.center = self.contentView.center;
[super layoutSubviews];
}
Which fixed everything. No more bar on the right side and both title and image are centered. Here is the final code in the TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
/*cell.textLabel.text = [NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]];
UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:#"image"]];
cell.imageView.image = cellImage;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];*/
[cell.cellImage setImage:[UIImage imageWithData:[moment valueForKey:#"image"]]];
[cell.cellTitle setText:[NSString stringWithFormat:#"%#", [moment valueForKey:#"name"]]];
return cell;
}
I still have commented out code in there, sorry about that. But thank you to all whole helped out in solving this problem!
DONT USE THE DEFAULT IMAGEVIEW FROM THE CELL... create a UIImageView... add it to cell's view at the desired position.. =)
GL HF
you can make custom cell for table view and load this cell in table view as you can place your contents in custom cell at desired position whatever you want.This is the best approach to make table view cell if your application requires complex design in table view.Example:static NSString *CustomCellIdentifier = #"CEll";//[NSString stringWithFormat:#"Cell%d",indexPath.row];
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; implement this in your cellforrowAtIndexPath method and add your cell nib -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self = (customCell *)[[[NSBundle mainBundle] loadNibNamed:#"customCell" owner:self options:nil] firstObject];}
}

How to properly Init a custom UITableviewCell?

I am using the following 2 methods to return a custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [self keyForIndexPath:indexPath];
UITableViewCell *cell;
if ([key isEqualToString:DoneButtonCellKey]) {
cell = [self [self doneButtonCellForIndexPath:indexPath];
return cell;
} else {
//code to return default cell...
}
}
Then:
- (DoneButtonCell *)doneButtonCellForIndexPath: (NSIndexPath *)indexPath {
DoneButtonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DoneButtonCellIdentifier forIndexPath:indexPath];
return cell;
}
What is the correct init method to use with the cell here so I can change some properties of the cell when it is initialized?
EDIT: I found the problem, as the init/awakeFromNib methods were not being called for me. I tracked down the error and it was that I had not changed the "Custom Class" from UITableViewCell to my custom class. Now awakeFromNib AND initWithCoder work as described below.
You can do your changes inside the DoneButtonCell's class, either in the
- (void)awakeFromNib
{
.. essential to call super ..
super.awakeFromNib()
//Changes done directly here, we have an object
}
Or the initWithCoder: method:
-(id)initWithCoder:(NSCoder*)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
//Changes here after init'ing self
}
return self;
}
If you're using Swift, remember the easy way to ensure a view is initialized when it is created is to use the didSet method. For example, to make a UIImageView into a round shape you could add code like this:
#IBOutlet weak var profileImageView: UIImageView! {
didSet {
// Make the profile icon circle.
profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
profileImageView.clipsToBounds = true
}
}
This is how I am initialising custom cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"FileTableViewCell";
FileTableViewCell *cell = (FileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"FileTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Configure the cell here...
// Configure the cell.
FileRepresentation* fileRepresentation = _fileList[indexPath.row];
cell.textLabel.text = [self userFilename:[fileRepresentation.fileName stringByDeletingPathExtension]];
cell.detailTextLabel.text = [fileRepresentation modifiedDate];
cell.accessoryView=nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.progressIndicator setHidden:YES];
cell.imageView.image = [UIImage imageNamed:_fileImageName];
// Disable any user interaction while processing a request
if (_fileIsOpen || _creatingDocument || _deletingDocument) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textColor = [UIColor grayColor];
} else {
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}
}
First try to dequeue a cell if possible using dequeueReusableCellWithIdentifier method of UITableView.
If cell is not available (nil) use [[NSBundle mainBundle] loadNibNamed:#"<#your custom cell nib name#>" owner:nil options:nil][0] to initialize it.
In your custom cell's .m file, implement initWithCoder: initializer for custom initialization code:
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
//your custom initialization code
return self;
}
This is the designated initializer that is called when any view is loaded from a nib with loadNibNamed, like a custom table view cell.

Custom UITableViewCell (IB) only shows in selected state

I made a custom UITableViewCell in Interface Builder (Storyboard) and imported it to my project via #import CustomTableViewCell.h.
Everything works fine, but the cell is only loaded in selected state.
I want the cell to be loaded in every row by init.
P.S. The slider and text field connections work fine. I also made all of the IB Connections.
CustomTableViewCell.m
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell
#synthesize sliderLabel, slider;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)getSliderValuesWithValue:(UISlider *)sender
{
sliderLabel.text = [NSString stringWithFormat:#"%i / 100", (int) roundf(sender.value)];
}
#end
Further Code
- (CustomTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Kriterium";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:#"%#", [listOfItems objectAtIndex:indexPath.row]];
return cell;
}
P.S. If I add some Buttons etc. programmatically in the above method it works. But I want to design the rows in IB. There has to be a solution.
Okay ... strange things happening here ... ;-) The problem was this line:
cell.textLabel.text = [NSString stringWithFormat:#"%#", [listOfItems objectAtIndex:indexPath.row]];
Leaving it out did the trick. I had to add another UILabel to my CustomCell which I fill with text.
CONCLUSION
Filling the standard UITableViewCell.textLabel.text seems to overwrite the PrototypeCells.
... too much customization hurts. ;-)
Thanks anyway! :)
Suggesting you to not go for IB. Just define those controls as property and in your init method- initWithStyle(CustomTableViewCell.m file) initialize UISlider with its default property:
UISlider *tempSlider = [[UISlider alloc] initWithFrame:frame];
tempSlider.selected = NO;
//define other properties as well
self.slider = tempSlider;
[self addSubview:self.slider];
[tempSlider release];
Besides you can also set cell selection style to none.
cell.selectionStyle = UITableViewCellSelectionStyleNone;

Resources