我有UITextField
,UITableView
和UIButton
。我将UITextField
的值存储在NSString
中,如下所示。当我按“完成”UIButton
时,我想将NSString
的值存储在UITableview
的第一个单元格中。当我在UITextField
中输入一个新字符串并重复该值应该存储在第二个单元格中的过程等等。
NSString *cellValues = textField.text;
- (UITableViewCell *)tableview:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
SampleTableview *cell;
}
答案 0 :(得分:0)
您的班级可以拥有NSMutableArray
属性(请注意,您应该遵守UITableViewDelegate
和UITableViewDataSource
协议):
@interface viewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *listOfStrings;
@end
确保在UITableView
方法上正确设置viewDidLoad
和数组(如果将Table View Controller
对象拖放到Storyboard中,则无需执行此操作此):
- (void)viewDidLoad
{
[super viewDidLoad];
self.listOfStrings = [[NSMutableArray alloc]init];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
设置tableView
委托方法,如下所示:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listOfStrings count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [self.listOfStrings objectAtIndex:indexPath.row];
return cell;
}
然后,当您按下UIButton
时,被调用的方法应该将UITextField
文本添加到数组并重新加载tableView
:
- (void)buttonPressed{
[self.listOfStrings addObject:self.textField.text];
[self.tableView reloadData];
}
希望这有帮助。