New to objective. I am trying to put 2 values into a table view. this is part of the code:
- (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.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
cell.textLabel.numberOfLines = 2;
NSString *desc= #"Alight Destination =";
cell.textLabel.text = desc,[alDescArray objectAtIndex:indexPath.row];
return cell;
}
for this line cell.textLabel.text = desc,[alDescArray objectAtIndex:indexPath.row];
the table only display Alight Destination = without adding in the values for [alDescArray objectAtIndex:indexPath.row];
What part did i do wrongly pls help
You should use
cell.textLabel.text=[NSString stringWithFormate:#"%# %#",desc,[alDescArray objectAtIndex:indexPath.row];
or
NSString *desc=[NSString stringWithFormart: #"Alight Destination = %#",[alDescArray objectAtIndex:indexPath.row] ];
cell.textLabel.text = desc;
`
Use following way...
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",desc,[alDescArray objectAtIndex:indexPath.row]];
I think it will be helpful to you.
guess subclassing UITableViewCell will be useful. find code below hope it will help..!
# INTERFACE FILE
#import <UIKit/UIKit.h>
#interface CustomTableViewCell : UITableViewCell {
UIImageView *imageView;
UILabel *titleLabel1;
UILabel *titleLabel2;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
- (void)setValuesForCell:(YourModal*)agent withIndexPath:(NSIndexPath*)indexPath;
#property(nonatomic,retain) UIImageView *imageView;
#property(nonatomic,retain) UILabel *titleLabel1;
#property(nonatomic,retain) UILabel *titleLabel2;
#end
#IMPLEMENTATION FILE
#implementation CustomTableViewCell
#synthesize titleLabel1,titleLabel2,imageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Your initialisation if any.
}
return self;
}
- (void)setValuesForCell:(YourModal*)modal withIndexPath:(NSIndexPath*)indexPath{
titleLabel1.text=modal.name;
titleLabel2.text=modal.description;
imageView.image=modal.image;
//Other values you want set from modal
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
[titleLabel1 release];
[titleLabel2 release];
//Other memory releases
}
#end
In your xib you can load the CustomTableViewCell as your class for viewing on tableview
Related
I have a TableViewCell subclass and I am trying to create a table with this cell type. However the table won't show the custom cells content.
The values of the cell derive from data core model. But there isn't an issue with it as I have also tried with simple literals and it still wouldn't work. Any help is appreciated.
Here is my code:
favCell.m
#import "favCell.h"
#implementation favCell
#synthesize view;
#synthesize namelbl;
#synthesize scorelbl;
#synthesize prodimage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(2,20, 20, 20)];
namelbl = [[UILabel alloc]initWithFrame:CGRectMake(3,20, 40, 40)];
scorelbl = [[UILabel alloc]initWithFrame:CGRectMake(70,20, 30, 30)];
[view addSubview:namelbl];
[view addSubview:scorelbl];
[view addSubview:prodimage];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
favTable.m
#import "favTable.h"
#import "ecoAppDelegate.h"
#import "favCell.h"
#interface favTable ()
#end
#implementation favTable
#synthesize favArr;
#synthesize managedObjectContext;
#synthesize fetchedResultsController;
#synthesize favName;
#synthesize favScore;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
favCell*cell = (favCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[favCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.namelbl.text = [favName objectAtIndex:indexPath.row];
cell.scorelbl.text = [favScore objectAtIndex:indexPath.row];
cell.prodimage.image = [UIImage imageNamed:#"test1.jpg"];
return cell;
}
I'm trying to create a UITableView in a UIViewController (I'd use UITableViewController but I want the UITableView to only use half of the screen space) that will create a new custom cell every time I press a button, and display the current time in that cell and the time since the last time I pressed the button (so the time in this cell minus the time in the cell above it).
The problem is, when I press the button, either nothing happens or, if I don't initialise the property I created for the UITableView, a blank cell appears.
My cell has 3 UILabel's in it, inOut, entryTime and delta, and the IBOutlet I created for the UITableView is timeTable. newEntry is the button. I have set my UITableView's datasource and delegate to my UIViewController, have "called" the protocols UITableViewDataSource, UITableViewDelegate on my UIViewController's header. I can't find what's wrong.
here's my code for the UIViewController:
#interface MainViewController ()
#property (strong, nonatomic) Brain *brain;
#end
#implementation MainViewController{
#private
NSMutableArray *_entryArray;
NSMutableArray *_timeSinceLastEntryArray;
}
#synthesize brain=_brain;
#synthesize timeTable=_timeTable;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_brain = [[Brain alloc] init];
_entryArray = [[NSMutableArray alloc] init];
_timeSinceLastEntryArray = [[NSMutableArray alloc] init];
_timeTable = [[UITableView alloc] initWithFrame:CGRectZero];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_entryArray count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier= #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row%2==0){
cell.inOut.text = [NSString stringWithFormat:#"Entrada %d", indexPath.row/2 +1];
cell.entryTime.text = [NSString stringWithFormat:#"%#", [_entryArray objectAtIndex:indexPath.row]];
if (indexPath.row==0){
cell.delta.text=#"";
}
else {
cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
}
}
else{
cell.inOut.text = [NSString stringWithFormat:#"SaĆda %d", (indexPath.row+1)/2];
cell.entryTime.text = [NSString stringWithFormat:#"%#",[_entryArray objectAtIndex:indexPath.row]];
cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
}
return cell;
}
- (IBAction)newEntry:(id)sender {
[_entryArray addObject:[self.brain currentTime]];
NSInteger numberOfEntrys= [_entryArray count];
if (numberOfEntrys >1){
NSString *timeSinceLastEntry = [_brain timeSince:[_entryArray objectAtIndex:(numberOfEntrys-2)]];
[_timeSinceLastEntryArray addObject:timeSinceLastEntry];
}
else {
[_timeSinceLastEntryArray addObject:#"0"];
}
[_timeTable reloadData];
}
#end
and for CustomCell:
#implementation CustomCell
- (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
}
#synthesize inOut=_inOut, entryTime=_entryTime, delta=_delta;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
_inOut = [[UILabel alloc]init];
_inOut.textAlignment = UITextAlignmentLeft;
_entryTime = [[UILabel alloc]init];
_entryTime.textAlignment = UITextAlignmentLeft;
_delta = [[UILabel alloc]init];
_delta.textAlignment = UITextAlignmentLeft;
[self.contentView addSubview:_entryTime];
[self.contentView addSubview:_inOut];
[self.contentView addSubview:_delta];
}
return self;
}
#end
Thanks for the help :)
I see you put the initialize code in
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
But you create object with
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
and you did not provided the Frame to the view that you created
_inOut = [[UILabel alloc]init];
_entryTime = [[UILabel alloc]init];
_delta = [[UILabel alloc]init];
Hope it helps you
Right Now I am making a twitter app and I am Using IFTweetLabel. I made a TableViewCell Subclass but When I Run it the label dosent show up.
Here is the .h:
#import <UIKit/UIKit.h>
#import "IFTweetLabel.h"
#interface twitterTextCell : UITableViewCell
#property (strong, nonatomic) IFTweetLabel *customtextLabel;
#end
And here is the implementation:
#import "twitterTextCell.h"
#implementation twitterTextCell
#synthesize customtextLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
customtextLabel.backgroundColor = [UIColor orangeColor];
self.customtextLabel.frame = CGRectMake(240, 60, 345, 109);
[self addSubview:customtextLabel];
NSLog(#"Custom Label Text: %#", customtextLabel.text);
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(UILabel *)textLabel
{
return nil;
}
Here is the code that is in the cellforrow in the tableviewdelegate:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
twitterTextCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[twitterTextCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [self.timeLineData objectAtIndex:indexPath.row];
if ([tweet objectForKey:#"text"] != nil) {
NSString *allText = [[tweet objectForKey:#"text"] stringByDecodingHTMLEntities];
cell.customtextLabel.numberOfLines = 4;
cell.textLabel.numberOfLines = 4;
cell.customtextLabel.text = allText;
cell.textLabel.text = allText;
cell.customtextLabel.linksEnabled = YES;
NSDictionary *whoPostedTweet = [tweet objectForKey:#"user"];
cell.detailTextLabel.text = [whoPostedTweet objectForKey:#"name"];
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#", [whoPostedTweet objectForKey:#"profile_image_url"]]];
[cell.imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.backgroundColor = [UIColor clearColor];
[cell.imageView.layer setCornerRadius:7.0f];
[cell.imageView.layer setMasksToBounds:YES];
return cell;
} else {
[self getTwitterData];
NSLog(#"nil called(else) ");
return cell;
}
return cell;
}
After all of this only the image and the detailtextview show up. I removing the default textview because If I don't the text shows up normally but it is not the IFTweetLabel. Another thing I have a question about is how do I reposition the detailtextabel so that it is Under the IFTweetLabel. Anything is greatly appreciated, I need help on both of these issues.
i think your problem is you are not allocating customTextLabel.
Just allocate the same before setting the background color in initWithStyle.
ie, self.customtextLabel = [[[IFTweetLabel alloc] init] autorelease];
I am having ViewAController:UITableViewController.I also have firstNameCell and lastNameCell like below
In ViewAController, I do
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 0)
return firstNameCell;
return lastNameCell;
}
My question : why my cells are nil even though they are connecting to the cell in xib
Please advice me on this issue.
Your cellForRowAtIndexPath method does not initialize the cell properly.
It should contain code this -
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
It's possible you are being called before the nib has finished loading. Check to see if your -awakeFromNib method has been called yet.
Please try this:
static NSString *CellIdentifier = #"Cell";
cellVideoOptions *cell =(cellVideoOptions *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
and then init cell with nib name.
Do it programmatically:
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
{
UILabel *firstName;
UILabel *lastName;
}
#property (nonatomic, retain) UILabel *firstName;
#property (nonatomic, retain) UILabel *lastName;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize firstName, lastName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
firstName = [[UILabel alloc]init];
firstName.textAlignment = UITextAlignmentLeft;
firstName.font = [UIFont boldSystemFontOfSize:18];
firstName.backgroundColor = [UIColor clearColor];
lastName = [[UILabel alloc]init];
lastName.textAlignment = UITextAlignmentLeft;
lastName.font = [UIFont boldSystemFontOfSize:14];
lastName.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:firstName];
[self.contentView addSubview:lastName];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+7 ,7, 240, 20);
firstName.frame = frame;
frame= CGRectMake(boundsX+125 ,25, 220, 20);
lastName.frame = frame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void)dealloc
{
[firstName release];
[lastName release];
[super dealloc];
}
#end
In your ViewAController with TableView, dont forget import CustomCell.h
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
if (indexPath.section == 0) {
cell.firstName.text = #"firstname";
// and or cell.firstName.frame = ...
} else {
cell.lastName.text = #"lastname"
// and or cell.lastName.frame = ...
}
return cell;
}
I'm trying to create my own custom cell, but for some reason, it isn't appearing. I read a lot of example, the code looks like (at least for me) equal to others. I used the interface builder to recreate it (I deleted the Default View, add a TableViewCell, put Identifier = CustomCell, and wire up the Label with valueLabel).
Can comeone help me?
Thanks in advance.
//CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell {
IBOutlet UILabel *valueLabel;
}
#property(nonatomic, retain) IBOutlet UILabel *valueLabel;
#end
//CustomCell.C
#import "CustomCell.h"
#implementation CustomCell
#synthesize valueLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[super dealloc];
}
#end
//Where I'm trying to use this cell. Here I imported the .h file (#import "CustomCell.h"):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.valueLabel.text = #"Any text";
return cell;
}
Your cellForRowAtIndexPath method belongs in the table view controller? Is that where it is located?