[英]Create new cell in table view
我需要在表视图控制器中创建一个新单元格。 我遇到的问题基本上是我需要控制该单元格将在左右文本中显示的内容(该单元格的样式为“ Right Detail”)。 我想将save.text显示在右侧,而saveName.text显示在左侧。 创建新单元格的代码应位于AccessData中。 希望你们对如何做到这一点有所了解。
这里有一些代码背景:
我基本上有一个生成随机数的数字生成器,还有一个保存该数字的按钮。 因此,当用户按下保存按钮时,将向用户显示一个屏幕,其中显示了文本输入,用户可以在其中输入标题。 然后,当按下完成按钮时,标题和随机数将作为新单元格保存在表格视图中。 标题将显示在单元格的左侧,随机数将显示在右侧。 我可能需要使用比已经完成的操作更多的数组,但这基本上是正在发生的事情。 我有所有用于数字生成器的代码,但是我只需要设置保存和表视图代码。 希望你们对如何使这项工作有所了解。
.h文件
NSString *saved;
NSString *savedName;
@interface SecondViewController : UIViewController
{
IBOutlet UILabel *display2;
IBOutlet UILabel *displayName;
NSMutableArray *list;
}
-(void)AccessData:(id)sender;
@property (strong,nonatomic) IBOutlet UITableView *tableView;
@end
.m文件
@interface SecondViewController ()
@end
@implementation SecondViewController
-(void)viewDidLoad{
[super viewDidLoad];
list = [[NSMutableArray alloc]initWithObjects:@"hej", nil];
}
-(void)AccessData:(id)sender {
if (savedData == 1) {
savedName = displayName.text;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIndentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier ];
}
cell.textLabel.text = [list objectAtIndex:indexPath.row];
return cell;
}
@end
只要您的模型发生更改,您只需在表上调用reloadData
。 在这种情况下,您的模型就是list
数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.