Label in UITableViewCell is not getting accessed in .m file - uitableview

I have a tableview in my view controller. I took one protype cell in which I created a label and connected it to the tableVieCell.h file. The problem is when I try to access the label in my view to get it loaded with the textfield input, I am not getting the label component. Please check. Here is my MyCell.h file.
#import <UIKit/UIKit.h>
#interface MyCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *label1;
#property (strong, nonatomic) IBOutlet UILabel *label2;
#property (strong, nonatomic) IBOutlet UITextField *celltextField;
#end
here is my viewController.h file.
#import <UIKit/UIKit.h>
#import "MyCell.h"
#interface ViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *contacts;
}
- (IBAction)leftBtn:(id)sender;
- (IBAction)rightBtn:(id)sender;
- (IBAction)nextBtn:(id)sender;
- (IBAction)SendBtn:(id)sender;
#property (strong, nonatomic) IBOutlet UITableView *tabV1;
#property (strong, nonatomic) IBOutlet UITextField *txtField;
#end
#import "ViewController.h"
#interface ViewController ()
{
MyCell *cell;
}
#end
#implementation ViewController
- (void)viewDidLoad {
contacts= [[NSMutableArray alloc]init];
[super viewDidLoad];
}
-(BOOL)textFieldShouldReturn:(UITextField *) textField{
[textField resignFirstResponder];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return contacts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// [cell.zoomButton addTarget:self action:#selector(navigateAction:) forControlEvents:UIControlEventTouchUpInside];
// cell.zoomButton.tag=indexPath.row;
cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// cell.textLabel.text=contacts[indexPath.row];
cell.label2.text=[contacts objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)leftBtn:(id)sender {
}
- (IBAction)rightBtn:(id)sender {
}
- (IBAction)nextBtn:(id)sender {
}
-(void)someMethod:(UIButton *)sender{
}
- (IBAction)SendBtn:(id)sender {
// if (_leftBtn.tag==1) {
// cell.label1.text=_txtField.text;
//
// }else{
// cell.label2.text=_txtField.text;
// }
[self->contacts addObject:_txtField.text];
// label2.text=_txtField.text;
[_tabV1 reloadData];
_txtField.text=#"";
}
#end

Change this
cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
to this
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];

Related

Subclass of UITableView indexPath nil

I'm trying to subclass a UITableView. However, I'm unable to get an indexPath that isn't nil. The tableView has custom cells.
Here is my TouchTableView.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> //I brought this in because I'm saving audio files in my app
#import "SimpleTableCell.h"
#protocol myTableViewDelegate;
#interface TouchTableView : UITableView <UITableViewDelegate, UITableViewDataSource, AVAudioRecorderDelegate, AVAudioPlayerDelegate, UITextViewDelegate, UITextFieldDelegate, UIAlertViewDelegate>
#property (nonatomic, weak) id<myTableViewDelegate> myDelegate;
#property (strong, nonatomic) NSMutableArray *sortedFiles;
#property (strong, nonatomic) NSString *simpleTableIdentifier;
#property (strong, nonatomic) SimpleTableCell *cell;
#property BOOL inverted;
-(void)refreshTable;
#end
#protocol myTableViewDelegate <NSObject>
- (void)selectedFile:(TouchTableView *)tableView withURL: (NSURL *) tableViewURL IndexPath:(NSIndexPath *)indexPath;
-(void)didDelete:(TouchTableView *)tableView IndexPath:(NSIndexPath *)indexPath;
-(void)setSortedFile:(TouchTableView *)tableView;
#end
I attach a longpress like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//bring in your custom cell here
simpleTableIdentifier = #"SimpleTableCell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell.textField setEnabled:NO];
//put in long press
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[longPressGesture setMinimumPressDuration:1.0];
[cell addGestureRecognizer:longPressGesture];
}
}
return cell;
}
Then I have the following method for when the longpress activates:
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
if (![cell.textField isEnabled]) {
// only when gesture was recognized, not when ended
if (gesture.state == UIGestureRecognizerStateBegan)
{
// get affected cell
cell = (SimpleTableCell *)[gesture view];
// get indexPath of cell
NSIndexPath *indexPath = [self indexPathForCell:cell];
[self selectRowAtIndexPath:indexPath animated:NO scrollPosition:0];
[cell.textField setEnabled:YES];
[cell.textField becomeFirstResponder];
}
}
}
In my viewController in viewDidLoad I have:
self.touchTableView = [[TouchTableView alloc] init];
[self.tableView setDelegate:self.touchTableView];
[self.tableView setDataSource:self.touchTableView];
self.touchTableView.myDelegate = self;
The problem is that the indexPath is always nil. You'll note that I'm calling self instead of self.tableView because self is the tableView. Is there a way to get the indexPath?
Sorry for the mess all. rmaddy was right. Ridiculous. The solution:
in my ViewController.h I set up:
#property (weak, nonatomic) IBOutlet TouchTableView *tableView;
Then I changed the last code in my example to
[self.tableView setDelegate:self.tableView];
[self.tableView setDataSource:self.tableView];
self.tableView.myDelegate = self;
[self.tableView refreshTable];
Now I'm getting the index path.

Custom UITableViewCell delegate isn't calling a method

I have two buttons in my custom-made UITableViewCell.
Each button click should call the desired segue.
I setup a delegate on a cell like this:
CustomCell.h
#protocol CustomCellDelegate
-(void)seeQuestions;
-(void)seeLessons;
#end
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) id <CustomCellDelegate> delegate;
- (IBAction)seeQuestions:(UIButton *)sender;
- (IBAction)seeLessons:(UIButton *)sender;
CustomCell.m
- (IBAction)seeQuestions:(UIButton *)sender {
[self.delegate seeQuestions];
}
- (IBAction)seeLessons:(UIButton *)sender {
[self.delegate seeLessons];
}
Now, in the ViewController class, where I have my UITableView, showing the custom cell, I import the CustomCell.h file, and connect delegates
ViewController.h
#import "CustomrCell.h"
#interface ViewController : UIViewController <CustomCellDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tableView;
-(void)seeQuestions;
-(void)seeLessons;
#end
ViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
cell.delegate = self;
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.title.text = [NSString stringWithFormat:#"%#",_topicsArray[indexPath.row]];
return cell;
}
-(void)seeQuestions
{
[self performSegueWithIdentifier:#"seeQuestions" sender:self];
}
-(void)seeLessons
{
[self performSegueWithIdentifier:#"seeLessons" sender:self];
}
But, when the I click the buttons, nothing happens. I am sure Im missing out some realy obvious thing, but after couple of hours staring at my code, i cannot seem to figure out what am I doing wrong.
Any suggestions ?

IOS: Not able to access my label in UICell

I am trying to create a table with prototype cell
for that i had taken a tableview and inserted a cell in table view
and in cell i had taken a label in which i want to set values from my Employee class.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:#"employeeeCell"];
if (!cell){
cell = [[RWTCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"employeeeCell"];
}
Employee *currentEmp=[employeeArray objectAtIndex:indexPath.row];
NSLog(#" %#",currentEmp.empName);
cell.lName.text=currentEmp.empName;
// cell.textLabel.text = currentEmp.empName;
return cell;
}
Above is my code
when i try to do
RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:#"employeeeCell"];
it always gives me null, and this line below never works
cell.lName.text=currentEmp.empName;
my Employee.m class
#import "Employee.h"
#implementation Employee
#synthesize empDepart,empDoj,empDob,empName,empSalary,empGender;
#end
and Employee.h
#import <Foundation/Foundation.h>
#interface Employee : NSObject
#property (nonatomic, retain) NSString *empName;
#property (nonatomic, retain) NSString *empSalary;
#property (nonatomic, retain) NSString *empDob;
#property (nonatomic, retain) NSString *empDoj;
#property (nonatomic, retain) NSString *empGender;
#property (nonatomic, retain) NSString *empDepart;
#end
EDIT
sorry i forgot to mention my cell class
and i am working on storyboard
#import "RWTCell.h"
#implementation RWTCell
#synthesize lName;
- (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
#import <UIKit/UIKit.h>
#interface RWTCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *lName;
#end
Take a look at the custom cell I am using in my application.
.h file of custom cell
#import <UIKit/UIKit.h>
#interface EvalHistoryCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *lblEvaluator;
#property (strong, nonatomic) IBOutlet UILabel *lblDate;
#property (strong, nonatomic) IBOutlet UILabel *lblStore;
#property (strong, nonatomic) IBOutlet UILabel *lblStatus;
#end
.m file of custom cell
#import "EvalHistoryCell.h"
#implementation EvalHistoryCell
- (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
in main viewcontroller where you want to use it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strEvalIdentifier = #"HistoryIdentifier";
EvaluationListEntity *objEvalEntity=[arrmEvalList objectAtIndex:indexPath.row];
EvalHistoryCell *cell = (EvalHistoryCell *)[tableView dequeueReusableCellWithIdentifier:strEvalIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"EvalHistoryCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lblEvaluator.text=objEvalEntity.strEvaluatorName;
return cell;
}
Try this.
Make sure you connected your custom cell with your view at RWTCell.xib
At your .h file
#property(nonatomic,strong) RWTCell *rwtcell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier= #"MyIdentifier";
RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell==nil)
{
[[NSBundle mainBundle] loadNibNamed:#"RWTCell" owner:self options:nil];
cell=self.rwtcell;
}
Employee *currentEmp=[employeeArray objectAtIndex:indexPath.row];
NSLog(#" %#",currentEmp.empName);
cell.lName.text=currentEmp.empName;
return cell;
}
Hope it helps you..

Set label for a custom cell

I have a custom table cell and I'm just trying to set the label, image, etc but for some reason it's not working.
Here is my code for my custom cell
BrowserMenuCell.h
#import <UIKit/UIKit.h>
#interface BrowseMenuCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIButton *wishListBUtton;
#property (strong, nonatomic) IBOutlet UIImageView *itemImage;
#property (strong, nonatomic) IBOutlet UILabel *itemLabel;
#property (strong, nonatomic) IBOutlet UILabel *itemPrice;
#property (strong, nonatomic) IBOutlet UITextField *quantityField;
#property (strong, nonatomic) IBOutlet UILabel *totalLabel;
- (IBAction)plusButton:(id)sender;
- (IBAction)cartButton:(id)sender;
- (IBAction)minusButton:(id)sender;
#end
BrowserMenuCell.m
#import "BrowseMenuCell.h"
#implementation BrowseMenuCell
- (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
}
- (IBAction)plusButton:(id)sender {
}
- (IBAction)cartButton:(id)sender {
}
- (IBAction)minusButton:(id)sender {
}
#end
Cell for row at index path
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BrowseMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemsCell"];
OfficeSupply *supply = [_items objectAtIndex:indexPath.row];
if(cell == nil)
{
NSArray * views = [[NSBundle mainBundle] loadNibNamed:#"BrowseMenuCell" owner:nil options:nil];
for (id obj in views){
if([obj isKindOfClass:[UITableViewCell class]])
{
BrowseMenuCell * obj = [[BrowseMenuCell alloc]init];
obj.itemLabel.text = supply.itemName;
cell = obj;
break;
}
}
}
return cell;
}
Have you tried using [self.tableView reloadData]?
To start with, don't create a new random instance of the cell after you have already created one from the NIB file. Change the code to this:
for (BrowseMenuCell *obj in views){
if([obj isKindOfClass:[BrowseMenuCell class]])
{
obj.reuseIdentifier = #"ItemsCell";
obj.itemLabel.text = supply.itemName;
cell = obj;
break;
}
}
That won't guarantee it works, but at least the NIB setup of your labels won't be getting thrown away.
If it still doesn't work. Debug. Check the frames of the labels. Check the label references are set (not nil).
This is the wrong way you create cell. Try this one:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BrowseMenuCell *cell = (BrowseMenuCell*)[tableView dequeueReusableCellWithIdentifier:#"ItemsCell"];
OfficeSupply *supply = [_items objectAtIndex:indexPath.row];
if (_cellObj == nil) {
[[NSBundle mainBundle] loadNibNamed:#"BrowseMenuCell" owner:nil options:nil];
}
cell.itemlabel.text = supply.itemName;
return cell;
}

Can't access the IBOutlets of my custom cell on cellForRowAtIndexPath

I made a custom cell with a XIB:
.h
#import <UIKit/UIKit.h>
#interface TWCustomCell : UITableViewCell {
IBOutlet UILabel *nick;
IBOutlet UITextView *tweetText;
}
#end
.m
#import "TWCustomCell.h"
#implementation TWCustomCell
- (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
And I load them in cellForRowAtIndexPath: in this way:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TWCustomCell *cell = (TWCustomCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:#"TWCustomCell" owner:nil options:nil];
for (id currentObject in topLevelObject) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (TWCustomCell*) currentObject;
break;
}
}
}
// Configure the cell...
cell.tweetText.text = [tweets objectAtIndex:indexPath.row];
return cell;
}
On cell.tweetText.text = [tweets objectAtIndex:indexPath.row];
On the dot after cell, Xcode tells me_ "Property 'tweetText' not found on object of type 'TWCustomCell *'; did you mean to access ivar 'tweetText'?" and tells me to replace it with
cell->tweetText.text. But there appears the error: "Semantic Issue: Instance variable 'tweetText' is protected". What do I have to do?
You didn't declare a property that will allow access to the IBOutlets outside of the class with the dot syntax.
Here's how i would do it:
in your .h file:
#property (nonatomic, readonly) UILabel *nick;
#property (nonatomic, readonly) UITextView *tweetText;
in the .m:
#synthesize nick, tweetText;
Or you could remove the ivar IBOutlets and declare the properties as retain and IBOutlets like this:
#property (nonatomic, retain) IBOutlet UILabel *nick;
#property (nonatomic, retain) IBOutlet UITextView *tweetText;
The problem was with my custom cell ivars:
#import <UIKit/UIKit.h>
#interface TWCustomCell : UITableViewCell {
//added here #public and you can access them now
#public
IBOutlet UILabel *nick;
IBOutlet UITextView *tweetText;
}
#end

Resources