I have project(AppDelegate, ViewController and UITableViewCell).
//ViewController.m(Excerpt)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableCell *cell = (TableCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//====A====
cell = [[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
//====B==== //cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIView *bgView = [[UILabel alloc] initWithFrame:CGRectZero];
cell.backgroundView = bgView;
for(UIView *view in cell.contentView.subviews){
view.backgroundColor = [UIColor clearColor];
}
}
cell.titleLabel.text = [NSString stringWithFormat:#"%# %i", #"row", indexPath.row];
if(indexPath.row % 2){
cell.backgroundView.backgroundColor = [UIColor whiteColor];
}else{
cell.backgroundView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.1];
}
return cell;
}
//TableCell.m(UITableViewCell class)
#import "TableCell.h"
NSString *CellIdentifier = #"CellIdentifier";
#implementation TableCell
#synthesize titleLabel;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
{
titleLabel = [[UILabel alloc] initWithFrame:frame];
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.frame = CGRectMake(10.0, 0.0, 320.0, 40.0);
[self.contentView addSubview:titleLabel];
self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
warning(initWithFrame:...iOS3) is generated in the part //===A===. Therefore I write //===B===. Nothing in cell.
I want to get rid warning.
initWithFrame:reuseIdentifier: is a deprecated method since iOS 3.0 (lot of time). Use the newer initWithStyle:reuseIdentifier: and the warning will go away.
UITableViewCell documentation
Related
So, this is driving me nuts...
Everything works great, unless my datasource: an NSMutableArrray: _names contains more items than there are rows visible in the UITableView: namesTable.
then all hell breaks loose... cells are overlaying one another, there are empty cells, repeating cells.
here is my setup code.
#interface DetailViewController ()
{
NSMutableArray *_names;
}
My viewDidLoad method:
- (void) viewDidLoad
{
[super viewDidLoad];
self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackOpaque];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(handleBack:)];
self.navigationItem.leftBarButtonItem = backButton;
if (!_names)
_names = [[NSMutableArray alloc] init];
}
and cellForRowAtIndexPath:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"MyCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
UITextField *nameTextField;
if (!cell || refreshCells)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
self.view.bounds.origin.y+10,
self.view.bounds.size.width-19,
26.0)];*/
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+10,
self.view.bounds.origin.y+4,
self.view.bounds.size.width-19,
34.0)];
}
nameTextField.adjustsFontSizeToFitWidth = YES;
nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
nameTextField.backgroundColor = [UIColor clearColor];
nameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
nameTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
nameTextField.textAlignment = UITextAlignmentLeft;
nameTextField.font = [UIFont fontWithName:#"Noteworthy-Bold" size:22.0];
nameTextField.keyboardType = UIKeyboardTypeDefault;
nameTextField.returnKeyType = UIReturnKeyDone;
nameTextField.clearButtonMode = UITextFieldViewModeNever;
nameTextField.delegate = self;
nameTextField.userInteractionEnabled = NO;
NSString *object = _names[indexPath.row];
nameTextField.text = [object description];
[cell.contentView addSubview:nameTextField];
cell.selectionStyle = UITableViewCellEditingStyleNone;
return cell;
}
can anybody, please tell show me what I am doing wrong?
You can use XIB to design your cell, instead of writing this much code. Also what is that refreshCells?? Also where are you adding data to your Array??
Better try to design the cell in XIB and IBOutlet it. Then load it in cellForRow...
In your code every time you are adding one textfield to your cell
[cell.contentView addSubview:nameTextField];
which is not necessary at all..
This could solve your problem.
I ended up subclassing UITableViewCell, and moving most of this code into:
#implementation DetailViewCustomCell
#synthesize nameTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
nameTextField = [[UITextField alloc] init];
nameTextField.adjustsFontSizeToFitWidth = YES;
nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
nameTextField.backgroundColor = [UIColor clearColor];
nameTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
nameTextField.textAlignment = UITextAlignmentLeft;
nameTextField.font = [UIFont fontWithName:#"Noteworthy-Bold" size:22.0];
nameTextField.returnKeyType = UIReturnKeyDone;
nameTextField.userInteractionEnabled = NO;
[self.contentView addSubview:nameTextField];
self.selectionStyle = UITableViewCellEditingStyleNone;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x+10;
CGFloat boundsY = contentRect.origin.x+4;
CGFloat boundsW = contentRect.size.width-19;
CGRect frame;
frame= CGRectMake(boundsX ,boundsY, boundsW, 34.0);
nameTextField.frame = frame;
}
and in my view controller:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DetailViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[DetailViewCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSString *object = _names[indexPath.row];
cell.nameTextField.text = [object description];
cell.nameTextField.delegate = self;
return cell;
}
this worked perfectly!
In my application i am trying to adjust the position of the textlabel and detailtextlabel in a tableview cell.I am doing like this `
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cell_with_arrow.png"]];
[cell setBackgroundView:img];
[img release];
}
cell.textLabel.frame = CGRectMake(0,0, 150, 40);
cell.detailTextLabel.frame = CGRectMake(155, 55, 150, 40);
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14.0];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text =#"name";
cell.detailTextLabel.text=#" status";
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
//cell.detailTextLabel.textAlignment = UITextAlignmenteft;
//cell.textLabel.textAlignment = UITextAlignmentLeft;
return cell;
}
`but seems to be having no effect.can anybody help me to point me where i am going wrong?
textLabel and detailTextLabel are readonly so you can not change them.
#property(nonatomic, readonly, retain) UILabel *textLabel
#property(nonatomic, readonly, retain) UILabel *detailTextLabel
See here
What I would suggest is that you could create your custom UITableViewCell instead, you will have more control over it's content and can add custom subviews in there as well.
Here are some tutorials for that:
From IB
From Code
you need to override layoutSubviews method
Create a customcell class and inherit with uitableviewcell
#try this code
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews{
//Set here your frame
self.textLabel.frame = CGRectMake(0, , 320, 30);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Why setIndentationLevel is not getting with cells subviews?
I have a UITableView. My cellForRowAtIndexPath is like below. The arrow which i added is not shifting with the textlabel. why?
How can I do this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"small_arrow.png"]];
img.frame = CGRectMake(5, 10, 7, 11);
[cell.contentView addSubview:img];
cell.textLabel.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"name"];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
Yeah !!! Its workin now.
I created a custom cell and its m file is like
#import "customecell.h"
#implementation customecell
#synthesize textLbl,img;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
textLbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 320, 20)];
textLbl.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:textLbl];
img = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 7, 11)];
img.image = [UIImage imageNamed:#"small_arrow.png"];
[self.contentView addSubview:img];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews{
[super layoutSubviews];
float indentPoints = self.indentationLevel * self.indentationWidth;
self.contentView.frame = CGRectMake(
indentPoints,
self.contentView.frame.origin.y,
self.contentView.frame.size.width - indentPoints,
self.contentView.frame.size.height
);
}
#end
and the cellForRowAtIndexPath is changed to
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
customecell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[customecell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLbl.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"name"];
cell.textLbl.font = [UIFont fontWithName:#"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
cell.indentationWidth = 25;
float indentPoints = cell.indentationLevel * cell.indentationWidth;
cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);
return cell;
}
I am having a weird issue with a custom UITableViewCell implementation that I have:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithPulseStyle:PNRTableViewCellAccessoryDefault reuseIdentifier:#"SavedStories"];
if (self) {
storyImageView_ = [[UIImageView alloc] initWithFrame:CGRectMake(kPadding, kPadding, kImageSize, kImageSize)];
storyImageView_.layer.borderWidth = 2.0;
storyImageView_.layer.borderColor = [UIColor whiteColor].CGColor;
storyTitleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyImageView_.frameRight + kPadding*1.5, storyImageView_.frameY, self.bounds.size.width - storyImageView_.frameWidth - 4*kPadding, 0)];
storyTitleLabel_.numberOfLines = 0;
storyTitleLabel_.lineBreakMode = UILineBreakModeClip;
storyTitleLabel_.font = [UIFont fontWithName:kProximaNovaBold size:14];
storyTitleLabel_.textColor = [UIColor whiteColor];
storyTitleLabel_.backgroundColor = [UIColor clearColor];
storyPublisherLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyTitleLabel_.frameX, storyTitleLabel_.frameHeight, storyTitleLabel_.frameWidth, 50)];
storyPublisherLabel_.numberOfLines = 1;
storyPublisherLabel_.font = [UIFont fontWithName:kProximaNova size:11];
storyPublisherLabel_.textColor = [UIColor colorWithWhite:140/255.f alpha:1.0];
storyPublisherLabel_.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:storyImageView_];
[self.contentView addSubview:storyTitleLabel_];
[self.contentView addSubview:storyPublisherLabel_];
}
return self;
}
-(void)setupCellViewWithSavedStory:(MyStory *) myStory
{
if (myStory.imageState == StoryImageAvailableOnDisk) {
storyImageView_.hidden = NO;
storyImageView_.image = myStory.image;
}
else {
storyImageView_.image = nil;
storyImageView_.hidden = YES;
}
CGSize titleSize = [myStory.title sizeWithFont:storyTitleLabel_.font constrainedToSize:CGSizeMake(storyTitleLabel_.frameWidth, kImageSize - kPadding) lineBreakMode:storyTitleLabel_.lineBreakMode];
[storyTitleLabel_ setFrameHeight:titleSize.height];
[storyTitleLabel_ setText:savedStory.title];
[storyPublisherLabel_ setText:savedStory.domain];
[self setNeedsLayout];
}
But for some reason is that I can't see image until I scroll down and scroll back up again. In other words the image is reused then I can see the image. All of the label text is showing just fine, it's just the image. Any idea why?
Try adding this code:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
}
Have you tried this?
if (cell == nil) {
cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
}
Here is the code I am struggling with, the first table cell doesnt display the accessary arrow, but other table cells work fine...
Below is the code for table cell1, other cells is also customized but work fine.
- (void) initialization
{
labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
labelTitle.font = [UIFont fontForMoreLikeResultTitle];
labelTitle.textColor = [UIColor blackColor];
labelTitle.numberOfLines = 1;
labelTitle.lineBreakMode = UILineBreakModeTailTruncation;
labelTitle.backgroundColor = [UIColor clearColor];
labelFulLAddress = [[UILabel alloc] initWithFrame:CGRectZero];
labelFulLAddress.font = [UIFont fontForMoreLikeResultDescription];
labelFulLAddress.textColor = [UIColor blackColor];
labelFulLAddress.numberOfLines = 1;
labelFulLAddress.lineBreakMode = UILineBreakModeTailTruncation;
labelFulLAddress.backgroundColor = [UIColor clearColor];
[[self contentView] addSubview:labelTitle];
[[self contentView] addSubview:labelFulLAddress];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
[self initialization];
}
return self;
}
- (void) layoutSubviews
{
float xOffset = 20.0f;
float yOffset = 10.0f;
float currentUsedHeight = yOffset;
labelTitle.text = documentTitle;
labelTitle.frame = CGRectMake(xOffset, currentUsedHeight,
320.0f - 2 * xOffset, 60.0f);
[labelTitle sizeToFitHeight];
[labelTitle sizeToFitWidth];
labelFulLAddress.text = #"99999 Bellevue Way NE, Bellevue WA";
currentUsedHeight += (yOffset + labelTitle.frame.size.height);
labelFulLAddress.frame = CGRectMake(xOffset, currentUsedHeight, 320.0f - 2 * xOffset, 60.0f);
[labelFulLAddress sizeToFitHeight];
[labelFulLAddress sizeToFitWidth];
}
Below is the code in view controller:
- (UITableViewCell *) createResultTableCell1:(UITableView *)tableView
{
static NSString *CellIdentifier = #"FirstMoreLikeResultCell";
FirstResultTableCell *cell = (FristResultTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[MoreLikeTableCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.documentTitle = self.documentTitle;
return cell;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row == 0)
{
cell = [self createResultTableCell1:tableView];
}
else
{
cell = [self createResultTableCell2:tableView cellForRowAtIndexPath:indexPath];
}
return cell;
}
Call [super layoutSubviews] from within your overridden layoutSubviews.