Custom UITableViewCell functions are called, but native table is presented
This is my code in the init function:
self.answersTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 45, 280, 500)];
self.answersTable.dataSource = self;
self.answersTable.delegate = self;
This is my code in the viewDidLoad function:
[self.answersTable registerClass:[AnswerTableViewCell class]
forCellReuseIdentifier:CellIdentifier];
This is my table delegate functions, which are called, and the cell is not nil in the end of it:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.question.answers count];
}
-(AnswerTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
AnswerObject* answer = self.question.answers[indexPath.row];
[cell setupAnswerTableViewCell:self.question answer:answer row:indexPath.row];
return cell;
}
And here is AnswerTableViewCell:
#import "AnswerTableViewCell.h"
#implementation AnswerTableViewCell
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0, 0, 280, 77);
self.answerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-20, self.frame.size.height)];
self.answerLabel.numberOfLines = 0;
self.answerLabel.textColor = [UIColor whiteColor];
self.answerLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:20];
self.answerToggle = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-50, 23, 30, 30)];
self.backgroundImage = [[UIImageView alloc]initWithFrame:self.frame];
[self addSubview:self.answerLabel];
[self addSubview:self.answerToggle];
[self addSubview:self.backgroundImage]; }
return self;
}
-(void)setupAnswerTableViewCell:(QuestionObject*)question
answer:(AnswerObject*)answer
row:(NSInteger)row{
self.question = question;
self.answer = answer;
self.row = row;
self.answerLabel.text = answer.answerText;
[self.answerToggle addTarget:self
action:#selector(flip:)
forControlEvents:UIControlEventTouchDown];
self.answerToggle.tag = [answer.answerID intValue];
if (self.row == 0) {
self.backgroundImage.image = [UIImage imageNamed:#"List_Top_Item_Not_Selected_612x113px.png"];
}
else if (self.row == ([self.question.answers count] - 1)){
self.backgroundImage.image = [UIImage imageNamed:#"List_Bottom_Item_Not_Selected_612x113px.png"];
}
else{
self.backgroundImage.image = [UIImage imageNamed:#"List_Item_Not_Selected_612x113px.png"];
}
}
So, whats wrong?
The problem is:
cell = [[AnswerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
specifically: UITableViewCellStyleDefault
You're asking it to create a Cell with the default style. Either just use [[AnswerTableViewCell alloc] init]; and set the reuse identifier. Or create your own init that takes in a reuse identifier without a style param
EDIT:
After the Cell code was added I believe the issue to be that you are using the init function to add your UI elements. These are most likely being overridden by the cells UIView contentView. Its the contentView you need to customise in order to add these elements.
This article will walk you through how to do this and using the drawRect method to customise the UIView.
http://blog.giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/
As stupid as it may sound, the thing that fixed it for me was:
self.answersTable.backgroundColor = [UIColor clearColor];
Related
Hi I am very new in iOS and I have created one tablelist on my Viewcontroller and here on my tablelist row I have added one UIlabel programatically, ok that's fine.
And I have added one UIbutton programatically on my "self.view".
Here my main requirement is when I click that button, I want to change UIlabel background color which was added on my tablelist row.
For this I have tried the code bellow, but UIlabel backgroudcolor is not changing.
Please help me.
my code:-
#import "ViewController.h"
#interface ViewController (){
UILabel *label;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
label.backgroundColor = [UIColor redcolor];
}
Your question was not clear if you want to change the label color on all cell or just some.
Anyway this color change must be made cellForRowAtIndexPath which is where the update / creation of the cell is made.
You can create a variable to inform if you want the color change or not and it check this on cellForRowAtIndexPath.
#implementation ViewController {
UITableView *tableView;
bool changeColor;
}
-(void)Method:(id)sender{
changeColor != changeColor;
[tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
UILabel label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
[cell.contentView addSubview:label];
}
if (changeColor){
label.backgroundColor = [UIColor redcolor];
}else{
label.backgroundColor = [UIColor clearColor];
}
return cell;
}
if you want change especific labels, you can create a nsarray to control what cell will be change
#interface ViewController (){
UILabel *label;
NSMutableArray *lableArray;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self tablelistCreation];
lableArray=#[].mutableCopy;
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[lableArray addObject:label];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
//
// label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
// label.backgroundColor = [UIColor clearColor];
// [cell.contentView addSubview:label];
return cell;
}
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
//button action
-(void)Method:(id)sender{
[lableArray makeObjectsPerformSelector:#selector(setBackground:) withObject:[UIColor redColor]];
// label.backgroundColor = [UIColor redcolor];
}
cell is Reusable so creation should put in cell == nil condition,at every run into the condition will creation a new UILabel ,if you want change all of them ,you should put them in a collection ,such as NSMutableArray...
import "ViewController.h"
#interface ViewController ()
{
UILabel *label;
BOOL isTouchButton;
}
#end
#implementation ViewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//UIbutton createtion
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button .backgroundColor = [UIColor orangecolor];
button.frame = CGRectMake(100, 440, 30, 30);
[self.view addSubview:button];
[self tablelistCreation];
}
//UItableView createtion
-(void)tablelistCreation{
tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, self.420) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//Delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];
label = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
label.tag = indexPath.row;
if(!isTouchButton)
{
cell.labelUserName.backgroundColor = [UIColor clearColor];
}
else{
cell.labelUserName.backgroundColor = [UIColor redColor];
}
[cell.contentView addSubview:label];
return cell;
}
//button action
-(void)Method:(id)sender
{
if(!isTouchButton)
{
[tablelistCreation reloadData];
isTouchButton = YES;
}
else{
[tablelistCreation reloadData];
isTouchButton = NO;
}
}
I have double items in my collectionView. How can I fix it?
I have UITableViewCell with UICollectionViewCell inside it.
I set elements size and direction based on its count.
My cell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.layout = [[UICollectionViewFlowLayout alloc] init];
self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
self.layout.itemSize = CGSizeMake(70, 70);
self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[IndexedCollectionView alloc] initWithFrame:CGRectMake(10, 250, 60, 320) collectionViewLayout:self.layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
}
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];
self.collectionView.frame = self.contentView.bounds;
}
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index
{
self.collectionView.dataSource = dataSourceDelegate;
self.collectionView.delegate = dataSourceDelegate;
self.collectionView.index = index;
[self.collectionView reloadData];
}
Delegate Methods
-(FeedCell *)collectionView:(IndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FeedCell *cell = (FeedCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = collectionView.frame;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
NSInteger count = [self test:collectionView].count;
if (count == 0)
{
return nil;
}
NSInteger imageIndex = 0;
if (count == 1)
{
[imageView setFrame:CGRectMake(0, 110, 310, 200)];
imageIndex = indexPath.item;
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
if (count == 2)
{
[imageView setFrame:CGRectMake(0, 110, 150, 200)];
imageIndex = indexPath.item;
imageView.contentMode = UIViewContentModeScaleAspectFit;
}
if (count == 3)
{
[imageView setFrame:CGRectMake(10, 100, 90, 90)];
imageIndex = indexPath.item;
}
if (count == 4)
{
[imageView setFrame:CGRectMake(10, 100, 130, 130)];//2lines
imageIndex = indexPath.item;
}
if (count == 5 || count == 6)
{
[imageView setFrame:CGRectMake(10, 100, 90, 90)];//2lines
imageIndex = indexPath.item;
}
if (count > 6)
{
[imageView setFrame:CGRectMake(10, 100, 100, 100)];//2lines
imageIndex = indexPath.item ;
//cell.layout.itemSize = CGSizeMake(50, 50);
}
imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];
[cell.contentView addSubview:imageView];
return cell;
}
The way you create the collectionView Cell in the table view Cell is not correct. Here are some references you can refer to create a collection view.
Collection View Cell Apple Reference.
Create Collection View without Storyboard
Refer to my answer here. You need to subclass your cells.
What is happening is you are dequeuing an existing cell, and then adding in another subview over it. Most of the users on here argue that the ONLY way to fix this is to go through the main subclassing method. It is faster and more "formal", however, you can add another tool to your toolbox:
Instead, you can test if the subview has already been added WITHOUT subclassing, and just stick it in a logic block. Here is what you can do:
BOOL doesContain = [cell.subviews containsObject:imageView];
if (!doesContain)
{
[cell.contentView addSubview:imageView];
}
imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];
Here is my code , it is very sample.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
_userView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
_userView.backgroundColor = [UIColor grayColor];
[self addSubview:_userView];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320, 420) style:UITableViewStyleGrouped];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.delegate = self;
_tableView.dataSource = self;
[self addSubview:_tableView];
// _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"aaa"];
if (!cell.backgroundView) {
NSLog(#"wrong");
}
return cell;
}
My table is grouped , the doc of Apple says that :
the cell.background : // Default is nil for cells in UITableViewStylePlain, and non-nil for UITableViewStyleGrouped. The 'backgroundView' will be added as a subview behind all other views.
but my cell.backgroundView = nil.
anyone help , thank you.
Hi i am almost new in programing.
I am faced with an error that I couldn't solve anyway. Even after comparing with anther solutions.
I have worked on it for about 3 days.
So let me completely describe my problem:
1.this is my implementation code:
#import "DocumentTableViewController.h"
#import "AddDocumentTableView.h"
#import "DB_document.h"
#implementation DocumentTableViewController
#synthesize managedObjectContext;
#synthesize btnAddDocument;
#synthesize fetchedObjects;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSManagedObjectContext *context = managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"DB_document" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSLog(#"%d",[fetchedObjects count]);
}
- (void)viewDidUnload
{
[self setBtnAddDocument:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int result = 0;
if (section == 0)
{
result = [fetchedObjects count] + 1;
}
else if (section == 1)
{
result = 1;
}
return result;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];
if (indexPath.section == 0 && indexPath.row == 0)
{
UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
lblMoney.text = #"amount";
lblMoney.textAlignment = UITextAlignmentCenter;
lblMoney.textColor = [UIColor blackColor];
lblMoney.backgroundColor = [UIColor clearColor];
lblMoney.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblMoney];
UILabel *lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 150, 40)];
lblDescription.text = #"description";
lblDescription.textAlignment = UITextAlignmentCenter;
lblDescription.textColor = [UIColor blackColor];
lblDescription.backgroundColor = [UIColor clearColor];
lblDescription.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDescription];
UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 40)];
lblDate.text = #"date";
lblDate.textAlignment = UITextAlignmentCenter;
lblDate.textColor = [UIColor blackColor];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDate];
UIButton *btnLine1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine1.frame = CGRectMake(80, 0, 1, 40);
[cell addSubview:btnLine1];
UIButton *btnLine2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine2.frame = CGRectMake(240, 0, 1, 40);
[cell addSubview:btnLine2];
return cell;
}
if (indexPath.section == 0 && indexPath.row != 0)
{
UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
lblMoney.text = [NSString stringWithFormat:#"%d",db_document.docAmount];
lblMoney.textAlignment = UITextAlignmentCenter;
lblMoney.textColor = [UIColor blackColor];
lblMoney.backgroundColor = [UIColor clearColor];
lblMoney.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblMoney];
UILabel *lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 150, 40)];
lblDescription.text = db_document.docDescription;
lblDescription.numberOfLines = 2;
lblDescription.textAlignment = UITextAlignmentCenter;
lblDescription.textColor = [UIColor blackColor];
lblDescription.backgroundColor = [UIColor clearColor];
lblDescription.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDescription];
UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 40)];
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:#"yyyy/mm/dd"];
lblDate.text = [NSString stringWithFormat:#"%#",[dateFormater stringFromDate:(NSDate *)db_document.docDate]];
lblDate.textAlignment = UITextAlignmentCenter;
lblDate.textColor = [UIColor blackColor];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblDate];
UIButton *btnLine1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine1.frame = CGRectMake(80, 0, 1, 40);
[cell addSubview:btnLine1];
UIButton *btnLine2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine2.frame = CGRectMake(240, 0, 1, 40);
[cell addSubview:btnLine2];
return cell;
}
if (indexPath.section == 1)
{
UILabel *lblMoney = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 40)];
lblMoney.text = #"";
lblMoney.textAlignment = UITextAlignmentCenter;
lblMoney.textColor = [UIColor blackColor];
lblMoney.backgroundColor = [UIColor clearColor];
lblMoney.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblMoney];
UILabel *lblTotalAmount = [[UILabel alloc] initWithFrame:CGRectMake(165, 0, 140, 40)];
lblTotalAmount.text = #"amounts";
lblTotalAmount.textAlignment = UITextAlignmentCenter;
lblTotalAmount.textColor = [UIColor blackColor];
lblTotalAmount.backgroundColor = [UIColor clearColor];
lblTotalAmount.font = [UIFont systemFontOfSize:12];
[cell addSubview:lblTotalAmount];
UIButton *btnLine = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLine.frame = CGRectMake(160, 0, 1, 40);
[cell addSubview:btnLine];
return cell;
}
return cell;
}
- (IBAction)btnAddDocument_click:(id)sender
{
AddDocumentTableView *addDocumentTableView = [[AddDocumentTableView alloc] init];
addDocumentTableView.managedObjectContext = managedObjectContext;
[self.navigationController pushViewController:addDocumentTableView animated:YES];
}
2.this is the error:
2012-06-16 15:25:31.696 Account5[5534:fb03] 2
2012-06-16 15:25:31.704 Account5[5534:fb03] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[_PFArray objectAtIndex:]: index (2) beyond bounds (2)'
*** First throw call stack:
3.Let me describe the program. I can save data to data base with core data but when i want to fetch the data program jumps out.I have to consider that I think NSManagedObjectContext fetched data because fetchedObjects Array has 2 data because I inserted in.
and I have to say that my RootViewController is DocumentTableViewController it means that exactly when I run the program it crashes. And if I want to run the app I must comment DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];
and after that app runs and I can insert data in another page.
I have to consider that when the app crashes it stops exactly on
DB_document *db_document = [fetchedObjects objectAtIndex:indexPath.row];
with green highlighted line.
thank you
Here is your problem:
In numberOfRowsInSection you add one to the fetchedResults array. Presumably, you want to add an additional row in that section. That is fine.
However, in cellForRowAtIndexPath you take indexPath.row as the index for your db_document. Obviously, in the last row it will crash. You have to first check in which row you are and then retrieve your db_document only if you need it for that particular row.
Problem is here :
result = [fetchedObjects count] + 1;
on numberOfRowsInSection
you add one row more than fetchedObjects count and when you on the cellForRowAtIndexPath
the fetchedObjects has for example 3 row and you get row 4 ,and this exception occored and you get out of range on this array.
I have a problem layout the objects in the code.
Here is a picture of current and desired status:
And here is the code
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"My Favorites", #"My Favorites");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
favoritesTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 364) style:UITableViewStylePlain];
[favoritesTable setHidden:NO];
[favoritesTable setDelegate:self];
[favoritesTable setDataSource:self];
[self.view addSubview:favoritesTable];
[favoritesTable release];
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"<#MyCell#>";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
NSInteger objectLocation = indexPath.row;
FoodData* food = (FoodData*)[resultsArray objectAtIndex:objectLocation];
cell.imageView.image = [UIImage imageNamed:#"paris.jpg"];
cell.imageView.frame = CGRectMake(270, 4, 40, 36);
cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;
UILabel* lblText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 20)];
UILabel* lblType = [[UILabel alloc] initWithFrame:CGRectMake(0, 21, 260, 20)];
[lblText setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
[lblType setFont:[UIFont fontWithName:#"Helvetica" size:9.0]];
lblType.textColor = [UIColor blueColor ];
[lblText setText:[food foodName]];
[lblType setText:[food foodType]];
[cell addSubview:lblText];
[cell addSubview:lblType];
[lblText release];
[lblType release];
return cell;
}
on ios5, making use of the prototype cell on the IB will be the easiest way out for you.
if you are not on ios 5, i do highly recommending using a custom tableviewcell.
The short solution to your issue can be found here in the link below.
UITableViewCell with image on the right?