![在此处输入图像描述][1]我想显示按下按钮的部分。而且,我想在部分标题上显示当前时间。并且,显示一个单元格部分。 同时按下按钮的日期,部分标题是我不想要两个。 我先做了一个静态的TableView。 我试图实现这个目的,然后在阅读各个网站时创建一个动态 TableView。代码被注释掉了。 请告诉我代码示例。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
//return [array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = @"section";
return title;
//return [array objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@ %i", @"row", indexPath.row];
return cell;
}
-(void)addButton
{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *strTime = [[NSString alloc] initWithFormat:@"%@",[formatter stringFromDate:date]];
array = [[NSMutableArray alloc] initWithCapacity:0];
[array addObject:strTime];
[array count];
}
最佳答案
不确定您到底打算在这里实现什么目标。假设您想要显示按下添加按钮的时间并在 TableView 的部分标题中显示该时间。代码将如下所示,
声明一个@property来保存当前类.h文件中的日期,
@property (nonatomic, retain) NSString *dateString;
在添加按钮操作中,
-(void)addButton
{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle]; //set the date format appropriately, Check SO for sample date formats.
self.dateString = [formatter stringFromDate:date];
[self.tableView reloadData];//reload your table view to show this date in title
}
您的部分标题委托(delegate)方法将如下所示,
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
return self.dateString;//modify this as per your requirement
else
return @"title";
}
调整上面的代码以满足您的要求。
关于iphone - 我想知道如何创建动态 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804313/