iphone - 我想知道如何创建动态 TableView

标签 iphone ios xcode uitableview

![在此处输入图像描述][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/

相关文章:

ios - SKNode runAction 滞后 :completion: in recursive method

ios - 如何使用 Gif 制作自定义事件指示器

iphone - 如何在 iOS 应用程序中使用自签名证书

iphone - 禁用 UINavigationItem 自定义右 View ?

iPhone使用iTunes传输文件,但不允许用户查看所有文档目录?

ios - Xcode 自动将信息图标/"Accessory Button"添加到我的表格单元格

ios - 添加应用程序组功能后清空授权文件

iphone - 使用通用应用内购买

iphone - 为什么 -animateWithDuration :delay:options:animations:completion: blocking the UI?

ios - 如何在从 iPhone 获得的加速度数据中找到所需的最大值?