【问题标题】:How to create a dynamic table view I want to know如何创建动态表格视图我想知道
【发布时间】:2012-12-10 15:49:36
【问题描述】:

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

【问题讨论】:

  • 对不起。我的回答可能很慢,因为已经午夜了。
  • 对不起。但是你的问题写得不是很好。很难理解你想知道什么。另外,如果是午夜,我猜你不住在美国,这可能就是你英语不好的原因。无论哪种方式,我都建议以更清晰简洁的方式重新编写您的问题。
  • 一张图片能说出一千个字,如果你能画一个简单的线框来说明你正在尝试做的事情,它可能会有所帮助。

标签: iphone ios xcode uitableview


【解决方案1】:

不确定您打算在这里实现什么。假设您要显示按下添加按钮的时间并在表格视图的部分标题中显示。代码将如下所示,

声明一个@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
}

您的部分标题委托方法将如下所示,

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return self.dateString;//modify this as per your requirement
    else 
        return @"title";
}

调整上述代码以满足您的要求。

【讨论】:

    猜你喜欢
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    相关资源
    最近更新 更多