Search code examples
uitableviewios7label

Label in UITableViewCell is not getting accessed in .m file


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

Solution

  • 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];