Programmatically adding two text fields to a tableViewCell - ios

I would like to programmatically add two text fields to an tableViewCell sub-class (more later) but having difficultly accessing the text-field from the view controller.
My table view sub class is:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
textField = [[UITextField alloc] initWithFrame:CGRectZero];
textField.clearsOnBeginEditing = NO;
textField.textAlignment = UITextAutocapitalizationTypeAllCharacters;
textField.returnKeyType = UIReturnKeyDone;
[self.contentView addSubview:textField];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect rect2 = CGRectMake(100.0, 10.0, 200, 30.0);
textField.text = #"testing";
[textField setFrame:rect2];
}
Within my view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
XTSessionCell_iPad *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[XTSessionCell_iPad alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
// ????
// cell.textField.text = #"enter";
return cell;
}
Any help very much appricatated.
Thanks

As others have mentioned in the comments, make the textView a readonly property in your XTSessionCell_iPad.h file. At a minimum your XTSessionCell_iPad.h file would look like:
#interface XTSessionCell_iPad : UITableViewCell
#property (nonatomic, readonly) UITextField *textField;
#end
And your XTSessionCell_iPad.m file would have a class extension to allow writing to that property:
#interface XTSessionCell_iPad()
#property (nonatomic, readwrite) UITextField *textField;
#end

Related

tablecell background only displays custom cell but displaying custom cell renders it unselectable

I have created a custom tableCell When I connect the xib via backgroundView to the file owner I successfully get items to appear in my table. The problem is I am not able to select a row and get a return.
If I unselect backgroundView then my custom cell does not populate but I am able to select a cell and extract a notification. I obviously want both of these items to work. Display my custom cell and have the ability to select a specific cell.
GameTableCell.m
#import "GameTableCell.h"
#implementation GameTableCell
#synthesize GameTime, AwayImage, HomeImage;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
/*
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
*/
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"GameTableCell" owner:self options:nil];
self = [topLevelObjects objectAtIndex:0];
return self;
}
#end
GameTable.h:
#import <UIKit/UIKit.h>
#interface GameTableCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *GameTime;
#property (weak, nonatomic) IBOutlet UIImageView *AwayImage;
#property (weak, nonatomic) IBOutlet UIImageView *HomeImage;
#end
HomePage.m:
#import "HomePage.h"
#import "PickSport.h"
#import "GameTableCell.h"
static NSString *CellIdentifier = #"GameTableCell";
NSString *headerText;
#interface HomePage ()
#end
#implementation HomePage
#synthesize ActiveChal,PendingChal;
-(IBAction)makeChallenge:(id)sender{
PickSport *second = [[PickSport alloc] initWithNibName:#"PickSport" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Arrays for filling table cells
_HomeImages=#[#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl", ];
_AwayImages=#[#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl", ];
_SportGameInfo=#[#"7:00pm",#"8:00pm",#"9:00pm",#"10:00pm",];
[self.PendingChal registerClass:[GameTableCell class] forCellReuseIdentifier:CellIdentifier];
[self.ActiveChal registerClass:[GameTableCell class] forCellReuseIdentifier:CellIdentifier];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//Set up the table props
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//number of rows in the table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// rows eqiv to length of array SportGameinfo
return _SportGameInfo.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier=#"GameTableCell";
GameTableCell *cell = (GameTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(tableView == PendingChal){
NSLog(#"in Pend table");
int row =[indexPath row];
cell.HomeImage.image=[UIImage imageNamed:[_HomeImages objectAtIndex:indexPath.row]]; //thumbnails objectAtIndex:indexPath.row]
cell.AwayImage.image=[UIImage imageNamed:[_AwayImages objectAtIndex:indexPath.row]];
cell.GameTime.text=[_SportGameInfo objectAtIndex:indexPath.row];
}
if(tableView == ActiveChal){
NSLog(#"in Active table");
int row =[indexPath row];
cell.GameTime.text=_SportGameInfo[row];
cell.HomeImage.image=[UIImage imageNamed:_HomeImages[row]];
cell.AwayImage.image=[UIImage imageNamed:_AwayImages[row]];
}
return cell;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 1. The view for the header
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
if(tableView == PendingChal){
headerText= #"Pending Challenges (waiting on you)";
}
else {
headerText= #"Active Challenges";
}
// 2. Set a custom background color and a border
headerView.backgroundColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
headerView.layer.borderColor = [UIColor colorWithWhite:0.5 alpha:1.0f].CGColor;
headerView.layer.borderWidth = 0;
// 3. Add a label
UILabel* headerLabel = [[UILabel alloc] init];
headerLabel.frame = CGRectMake(0, 0, tableView.frame.size.width -0, 20);
headerLabel.backgroundColor = [UIColor blackColor];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16.0];
headerLabel.text = headerText;
headerLabel.textAlignment = NSTextAlignmentLeft;
// 4. Add the label to the header view
[headerView addSubview:headerLabel];
// 5. Finally return
return headerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didSelectRowAtIndexPath");
/*UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:#"You've selected a row" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];*/
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:[_SportGameInfo objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display the Hello World Message
[messageAlert show];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"willSelectRowAtIndexPath");
return indexPath;
}
#end
HomePage.h:
#import <UIKit/UIKit.h>
#interface HomePage : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *PendingChal;
#property (strong, nonatomic) IBOutlet UITableView *ActiveChal;
#property (nonatomic, strong)NSArray *HomeImages;
#property (nonatomic, strong)NSArray *AwayImages;
#property (nonatomic, strong)NSArray *SportGameInfo;
#end
And to be complete here are the connection of my HomePage.xib
Put aside your app's weird behavior I notice you are not rendering the table cell in the usual way. So first try to render it in usual way as this :
register nib in viewDidLoad because you create your cell in xib
- (void)viewDidLoad {
[super viewDidLoad];
// Arrays for filling table cells
_HomeImages=#[#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl",#"ic_bengals_nfl", ];
_AwayImages=#[#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl",#"ic_bears_nfl", ];
_SportGameInfo=#[#"7:00pm",#"8:00pm",#"9:00pm",#"10:00pm",];
[self.PendingChal registerNib:[GameTableCell nib] forCellReuseIdentifier:CellIdentifier];
[self.ActiveChal registerNib:[GameTableCell nib] forCellReuseIdentifier:CellIdentifier];
}
dequeue reusable cell in - tableView:cellForRowAtIndexPath: and return it
you don't need load nib in tableViewCell's init method remove
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"GameTableCell" owner:self options:nil];
self = [topLevelObjects objectAtIndex:0];

UITextField delegate in custom UITableViewCell

I have custom cell prototype in my storyboard called YesNoTableViewCell. In that cell, I have UITextField.
I want to have the delegate methods of UITextField in my custom cell class.
Here is my code:
MyController
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"YesNoTableViewCell";
YesNoTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[YesNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
YesNoTableViewCell.m
#interface YesNoTableViewCell : UITableViewCell <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *yesNoInput;
#end
YesNoTableViewCell.h
#import "YesNoTableViewCell.h"
#implementation YesNoTableViewCell
#synthesize yesNoInput = _yesNoInput;
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_yesNoInput.delegate = self;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
But realized this never called:
(id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
because of:
YesNoTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
never returns nil.
Is there any suggestion for this?
it never return cell nil as you implemented
if (cell == nil){
cell = [[YesNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
so just remove this code

Weird issue in UILabel

I'm facing a weird issue in UILabel. If you look at the Image there is a border appears on right corner of my UILabel. I m subclassing UILabel to add some string properties.
This issue is inconsistent. happening in Landscape or in Portrait.
Anyone faced such issue/ What could be the issue? Am I doing something wrong?
I m sub classing it in below way:
#interface MyLabel : UILabel
#property (nonatomic, strong) NSString *strObjID;
#end
#implementation MyLabel
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
}
#end
this is how I add in UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *strId = #"layoutCell";
MyLabel *lblRecord;
UITableViewCell *layoutCell = [tableView dequeueReusableCellWithIdentifier:strId];
if (layoutCell == nil) {
layoutCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strId];
[layoutCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
lblRecord = [[MyLabel alloc] initWithFrame:CGRectMake(11, 10, 980,70)];
[layoutCell addSubview:lblRecord];
}
[lblRecord.textLabel setText:[arrListOfLayouts objectAtIndex:indexPath.row]];
return layoutCell;
}

Empty custom prototype cell

I'm new in iPhone development and have problem with prototype cell.
In my storyboard, I have navigation controller. I pushed it with view controller (as main window), it has a button, when I click it I open tableView controller with custom prototype cell
- (IBAction)searchClick:(id)sender {
CNCarTableController *car = [[CNCarTableController alloc]init];
[self.navigationController pushViewController:car animated:TRUE];
}
But when it opens it has empty rows
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: ( NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TransportCell";
CNTransportCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(cell == nil){
cell = [[CNTransportCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.textLabel.text = #"bmv";
cell.name = (UILabel*) [cell viewWithTag:100];
[cell.name setText:#"a text"];
cell.number.text = #"number";
cell.car.text = #"car";
return cell;
}
But if will use cell.textLabel.text = #"bmv"; all works ok. But for custom cell way with tags or cell.lbl.text don't work. What is wrong there? Can it be a nuance of navigation controller with view controller?
Custom cell code:
#interface CNTransportCell : UITableViewCell
#property (nonatomic,weak) IBOutlet UILabel *name;
#property (nonatomic,weak) IBOutlet UILabel *number;
#property (nonatomic,weak) IBOutlet UILabel *car;
#end
#implementation CNTransportCell
#synthesize name;
#synthesize number;
#synthesize car;
- (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
}
#end
you have done small mistake there. Your custom cell class is "CNPlayerCell" & you are allocating new cells from "CNTransportCell" class.
One more thing, have you assigned custom class to your prototype cell in IB? (In identity inspector)

ios custom cell on table not showing image

I have a custom cell loaded from my table, with an image and a label the label shows ok but the image doesn't show
CustomCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: (NSString*)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self initLabels];
CGRect vintageScreenRect = CGRectMake(25, 0.0f, 100, 100);
self.iconImage = [[UIImage alloc]init];
UIImageView *vintageScreen = [[UIImageView alloc] initWithFrame:vintageScreenRect];
//[vintageScreen setImage:[UIImage imageNamed:#"vidButtonImg.png"]];
// [vintageScreen setImage:self.iconImage];
[vintageScreen setImage:[UIImage imageNamed:self.tingo]];
vintageScreen.opaque = YES; // explicitly opaque for performance
[self.contentView addSubview:vintageScreen];
[vintageScreen release];
NSLog(#"tingo ::%#", self.tingo);
}
return self;
}
UsingTable.m
- (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];
cell.tingo = [NSString stringWithFormat:#"picsButtonImg.png"];
cell.iconName.text = #"d";
}
return cell;
}
So the label shows ok , but the image doesn't show, I have tried with sending an uiImage and an NSString,
what is missing? thanks!
self.tingo probably is nil in initWithStyle method. You are asigning self.tingo property after initWithStyle call.
That cause [UIImage imageNamed:self.tingo] is also nil, and the image simply doesn't exist.
You can fix it for example by custom setter of tingo property, or by making vintageScreen as property and set image "from outside".
Your variable tingo is null when you first instantiate the cell. Doing cell.tingo = [NSString stringWithFormat:#"picsButtonImg.png"]; sets it to a value, but then your cell never knows to reset the image.
Move vintageScreen into a variable on your cell class, then override your tingo setter to reload the image if you want to keep the code you have in cellForRowAtIndexPath:
CustomCell.h
#interface CustomCell : UITableViewCell {
UIImageView *vintageImage;
NSString *tingo;
}
#property (nonatomic, retain) UIImageView *vintageImage;
#property (nonatomic, retain) NSString *tingo;
#end
CustomCell.m
#implementation CustomCell
#synthesize vintageImage, tingo;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self initLabels];
[self addSubview:[self vintageScreen]];
}
return self;
}
-(UIImageView *)vintageScreen {
if(vintageScreen == nil) {
CGRect vintageScreenRect = CGRectMake(25, 0.0f, 100, 100);
[self setVintageScreen:[[[UIImageView alloc] initWithFrame:vintageScreenRect] autorelease]];
[vintageScreen setImage:[UIImage imageNamed:self.tingo]];
vintageScreen.opaque = YES;
}
return vintageScreen;
}
-(void)setTingo:(NSString *)newTingo {
[tingo release];
tingo = [newTingo retain];
[[self vintageScreen] setImage:[UIImage imageNamed:tingo]];
}

Resources