Ios 在视图控制器中设置表视图
这是我的问题。我创建了一个视图控制器,并在其中插入了一个表视图(带有单元格)。在视图控制器的底部还有一个文本字段。目的是将我在文本字段中写的文本放入单元格。我尝试了很多东西,但没有结果。 有人能帮我吗 这是我的代码(我无法显示NSLog)Ios 在视图控制器中设置表视图,ios,uitableview,uiviewcontroller,delegates,Ios,Uitableview,Uiviewcontroller,Delegates,这是我的问题。我创建了一个视图控制器,并在其中插入了一个表视图(带有单元格)。在视图控制器的底部还有一个文本字段。目的是将我在文本字段中写的文本放入单元格。我尝试了很多东西,但没有结果。 有人能帮我吗 这是我的代码(我无法显示NSLog) My.h @接口RBChatViewController:UIViewController @属性(强,非原子)IBOutlet UITextField*entryTextField; @属性(强,非原子)IBUITableView*tableview; 我的
My.h
@接口RBChatViewController:UIViewController
@属性(强,非原子)IBOutlet UITextField*entryTextField;
@属性(强,非原子)IBUITableView*tableview;
我的
#导入“RBChatViewController.h”
@接口RBChatViewController()
@结束
@视图控制器的实现
-(无效)viewDidLoad
{
[超级视图下载];
self.entryTextField.delegate=self;
self.tableview.delegate=self;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
NSLog(“部门数量”);
返回1;
}
您应该拥有表视图
和文本字段属性,
必须实现
tableView
的委托
和数据源
方法。并在cellforrowatinexpath
中设置cell.titleLabel.text=self.textField.text
,并调用[tableView reloadata]代码>来自textfielddichangetext
,或者如果您有某个按钮,请在该按钮操作中添加重新加载代码。很难在没有看到代码的情况下告诉您发生了什么,但此代码应该可以工作
-(void)addTextToTable {
//method runs when you push a button to say i am done writing in text field
NSString *textFeildText = self.textFeild.text; //create string of textfeild text
[self.mutableArray addObject:textFeildText]; //add string to dataSource array
[self.table reloadData]; // update table with new data to show the new string that is created
}
编辑这里是您需要添加的内容
My .h
@interface RBChatViewController : UIViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITextField *entryTextField;
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic) NSMutableArray *mutArray; // array of object that will be in the tableview
My .m
#import "RBChatViewController.h"
@interface RBChatViewController ()
@end
@implementation RBChatViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.entryTextField.delegate=self;
self.tableview.delegate=self;
self.tableview.dataDelegate = self; // ADD THIS, this means that the data for the tableview is gather from here This will also display your NSLog
_mutArray = [[NSMutableArray alloc] init]; //create your mutable array
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@" numberofsections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//tells how many rows in your tableview based on object in array
return _mutArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSString *string = [_mutArray objectAtIndex:indexPath.row];
cell.textLabel.text = string;
return cell;
}
My.h
@接口RBChatViewController:UIViewController
@属性(强,非原子)IBOutlet UITextField*entryTextField;
@属性(强,非原子)IBUITableView*tableview;
@属性(非原子)NSMutableArray*mutArray;//将在tableview中的对象数组
我的
#导入“RBChatViewController.h”
@接口RBChatViewController()
@结束
@视图控制器的实现
-(无效)viewDidLoad
{
[超级视图下载];
self.entryTextField.delegate=self;
self.tableview.delegate=self;
self.tableview.dataDelegate=self;//添加这个,这意味着tableview的数据是从这里收集的,这也将显示您的NSLog
_mutArray=[[NSMutableArray alloc]init];//创建可变数组
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
NSLog(“部门数量”);
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//根据数组中的对象指示tableview中的行数
返回_mutArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:单元标识符];
NSString*string=[\u mutary objectAtIndex:indexath.row];
cell.textlab.text=字符串;
返回单元;
}
您需要发布您尝试过的内容,并解释您遇到的问题。是的,我忘记了self.tableview.datasource=self;
My .h
@interface RBChatViewController : UIViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITextField *entryTextField;
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic) NSMutableArray *mutArray; // array of object that will be in the tableview
My .m
#import "RBChatViewController.h"
@interface RBChatViewController ()
@end
@implementation RBChatViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.entryTextField.delegate=self;
self.tableview.delegate=self;
self.tableview.dataDelegate = self; // ADD THIS, this means that the data for the tableview is gather from here This will also display your NSLog
_mutArray = [[NSMutableArray alloc] init]; //create your mutable array
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@" numberofsections");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//tells how many rows in your tableview based on object in array
return _mutArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSString *string = [_mutArray objectAtIndex:indexPath.row];
cell.textLabel.text = string;
return cell;
}