2012-12-10 132 views
0

![我在这里输入图片] [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]; 
} 
+0

对不起。答案我可能会很慢,因为它已经是午夜了。 – onion

+0

对不起。但是你的问题写得不是很好。很难理解你想知道什么。另外,如果是午夜,我猜你并不住在美国,这可能是你英语不好的原因。无论哪种方式,我都会建议以更清晰简洁的方式重新撰写您的问题。 – Josiah

+1

一张图片可以说出千言万语,如果您可以绘制您正在尝试做的简单线框图,可能会有所帮助。 – Imran

回答

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"; 
} 

调整上述代码,以适应您的要求。