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;
}
Related
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
I would like to make rounded corners for the first and last cell of UITableView. I have seen from this postCustom Rounded corners that you can use custom cells in a xib file, set their unique identifiers like: #"beginCell", #"middleCell", #"endCell". I do not want to use custom cells. Is there any other way to do this?
For example:
if (cell.count == 0 or cell.count == last)
{
cell.layer.cornerRadius = 10;
}
Something like this. But there is no property called count. Is there any other property?
EDITED:
ViewController.m
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize tableItems,images;
- (void)viewDidLoad
{
[super viewDidLoad];
tableItems = [[NSArray alloc] initWithObjects:#"Item1",#"Item2",#"Item3",nil];
images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"clock.png"],[UIImage imageNamed:#"eye.png"],[UIImage imageNamed:#"target.png"],nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Required Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Step 1:Check whether if we can reuse a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
//If there are no new cells to reuse,create a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:#"cell"];
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = v;
//changing the radius of the corners
//cell.layer.cornerRadius = 10;
}
//Set the image in the row
cell.imageView.image = [images objectAtIndex:indexPath.row];
//Step 3: Set the cell text content
cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];
//Step 4: Return the row
return cell;
}
#pragma mark - Optional Methods
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.backgroundColor = [ UIColor greenColor];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
NSString *selectedRow = [tableItems objectAtIndex:indexPath.row];
secondViewController.selectedRow = selectedRow;
//[self.navigationController pushViewController:secondViewController animated:YES];
[self presentViewController:secondViewController animated:YES completion:nil];
[secondViewController printRowNumber:indexPath.row];
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
self = [super initWithFrame:frame style:style];
return self;
}
#end
Just change your UITableView initialization style to Grouped like that:
yourTableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
This will make your first cell rounded from top and last cell rounded from bottom.
EDIT in case of not using UITableViewController initializer:
#interface YourViewController
#property (strong, nonatomic) UITableView *tableView;
#end
#implementation YourViewController
- (id)initWithFrame:(CGRect)frame
{
if (self = [super init])
{
...
tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
...
[self.view addSubview:tableView];
}
return self;
}
#end
Assuming you're putting this code in tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, you can do it like this:
if (indexPath.row == 0 || indexPath.row == ([indexPath length]-1))
{
cell.layer.cornerRadius = 10;
}
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
I have a tab based application. On the first tab I have several views one of which is a navigation controller. My goal is to load this list view(working) then goto a detailview on the cell click(not working).
On the cell click I am logging
NSLog(#"%#",self.navigationController);
which is returning a null. I can only assume that I have not done something right with setting up the navigation controller but I do not know what.
on button click the table view is called:
#import "Reviews.h"
#import "XMLParser.h"
#import "Detailed_Review.h"
#implementation Reviews
#synthesize reviewsTableView;
XMLParser *xmlParser;
CGRect dateFrame;
UILabel *dateLabel;
CGRect contentFrame;
UILabel *contentLabel;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[xmlParser reviews] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Review_Object *currentReview = [[xmlParser reviews] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
CGRect contentFrame = CGRectMake(10, 2, 265, 30);
UILabel *contentLabel = [[UILabel alloc] initWithFrame:contentFrame];
contentLabel.tag = 0011;
contentLabel.numberOfLines = 2;
contentLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:contentLabel];
CGRect reviewFrame = CGRectMake(10, 40, 265, 10);
UILabel *reviewLabel = [[UILabel alloc] initWithFrame:reviewFrame];
reviewLabel.tag = 0012;
reviewLabel.font = [UIFont systemFontOfSize:10];
[cell.contentView addSubview:reviewLabel];
}
UILabel *contentLabel = (UILabel *)[cell.contentView viewWithTag:0011];
contentLabel.text = [currentReview rating];
UILabel *dateLabel = (UILabel *)[cell.contentView viewWithTag:0012];
dateLabel.text = [currentReview review];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Detailed_Review *dvController = [[Detailed_Review alloc] initWithNibName:#"Detailed_Review" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
NSLog(#"%#",self.navigationController);
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
xmlParser = [[XMLParser alloc] loadXMLByURL:#"http://www.mywebsite.com/api/reviews/xml.php?page=1"];
self.title = #"Reviews";
}
- (void)viewDidUnload
{
[self setReviewsTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (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 != UIInterfaceOrientationPortraitUpsideDown);
}
#end
xib photos:
.h file:
#import <UIKit/UIKit.h>
#import "XMLParser.h"
#import "Review_Object.h"
#interface Reviews : UIViewController
#property (retain, nonatomic) IBOutlet UITableView *reviewsTableView;
#end
You omitted the part where the navigationController is allocated and initialized. Can you add that, including the relevant part from the .h file too?
Either create it with IB or programatically, see https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerPGforiOSLegacy/NavigationControllers/NavigationControllers.html#//apple_ref/doc/uid/TP40011381-CH103-SW27
I am working in a app and need to add button in cell by customcell.
Can any one help me for this?
Thanks in advance
You add a button in Customcell so use the following code.
First of all , you add new file with subclass of UITableViewCell and then
Customcell.h file
#interface CustomCell : UITableViewCell
{
UIButton *customButton;
}
#property (nonatomic , retain) UIButton *customButton;
Customcell.m
#implementation CustomCell;
#synthesize customButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:#"phonecall.png"] forState:UIControlStateNormal];
}
return self;
}
// set layout in subview
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGFloat boundY = contentRect.origin.y;
CGRect frame1;
frame1 = CGRectMake(boundsX+10, boundY+58, 19, 18);
customButton.frame = frame1;
}
#import "CustomCell.h"
In cellForRowAtIndexPath method
// Customize the number of cells in the table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell= [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell.customButton addTarget:self action:#selector(callAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}