.m 文件中未访问 UITableViewCell 中的标签
Label in UITableViewCell is not getting accessed in .m file
我的视图控制器中有一个表视图。我拿了一个原型单元格,在其中创建了一个标签并将其连接到 tableVieCell.h 文件。问题是当我尝试访问视图中的标签以加载文本字段输入时,我没有获得标签组件。请检查。这是我的 MyCell.h 文件。
#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
这是我的 viewController.h 文件。
#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
改变这个
cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
至此
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
我的视图控制器中有一个表视图。我拿了一个原型单元格,在其中创建了一个标签并将其连接到 tableVieCell.h 文件。问题是当我尝试访问视图中的标签以加载文本字段输入时,我没有获得标签组件。请检查。这是我的 MyCell.h 文件。
#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
这是我的 viewController.h 文件。
#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
改变这个
cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
至此
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];