2012-01-12 37 views
1

我在iPhone中设计了我的UITableViewCell。不幸的是我需要重新设计Cell。我会解释我的问题。我以前的/当前小区设计象下面这样:我如何设计UITableView iPhone中的单元格动态?

Header(11.1.2012) //single row 
event1 
time1 
Header(12.1.2012) //single row 
event1 
time1 
Header(13.1.2012) //single row 
event1 
time1 
Header(13.1.2012) //single row 
event2 
time2 
Header(13.1.2012) //single row 
event3 
time3 

报头(2012年1月11日) 事件1处于TableView中小区的辛格运河行。但是,我需要改变设计是这样的:

Header(11.1.2012) //single row 
event1 
time1 
Header(12.1.2012) //single row 
event1 
time1 
Header(13.1.2012) //single row 
event1 
time1 
event2 
time2 
event3 
time3 

我真的很困惑,如何做到这一点?任何人都可以请建议任何想法和示例代码来做到这一点?

- (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] autorelease]; 


     UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]; 
     dateView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"transactionHeader.png"]]; 

     UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 30)]; 
     headerLabel.tag = 100; 
     headerLabel.backgroundColor = [UIColor clearColor]; 
     headerLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
     headerLabel.font = [UIFont boldSystemFontOfSize:16]; 
     headerLabel.highlightedTextColor = [UIColor whiteColor]; 
     [dateView addSubview: headerLabel]; 
     [cell.contentView addSubview:dateView]; 

     UILabel *eventLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 25)]; 
     eventLabel.tag = 101; 
     eventLabel.backgroundColor = [UIColor clearColor]; 
     eventLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
     eventLabel.font = [UIFont boldSystemFontOfSize:14]; 
     eventLabel.highlightedTextColor = [UIColor whiteColor]; 
     [cell.contentView addSubview: eventLabel]; 

     UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, 300, 25)]; 
     timeLabel.tag = 102; 
     timeLabel.backgroundColor = [UIColor clearColor]; 
     timeLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
     timeLabel.highlightedTextColor = [UIColor whiteColor]; 
     [cell.contentView addSubview: timeLabel]; 
    } 

    UILabel * headerLabel = (UILabel *) [cell.contentView viewWithTag:100]; 
    headerLabel.text = [headerDateArray objectAtIndex:indexPath.row]; 

    UILabel *eventLabel = (UILabel *) [cell.contentView viewWithTag:101]; 
    eventLabel.text = [eventArray objectAtIndex:indexPath.row]; 

    UILabel * timeLabel = (UILabel *) [cell.contentView viewWithTag:102]; 
    timeLabel.text = [timeArray objectAtIndex:indexPath.row]; 

    return cell; 
} 

当前我在我的项目中使用此代码。我需要改变什么?谢谢。

+0

请提供示例代码 – 2012-01-12 13:50:13

+0

@AlexCoplan我附上了我当前的代码。我想改变我的新行设计的代码。请帮帮我。谢谢。 – 2012-01-12 14:04:29

回答

0

嗨感谢所有人的回答和查看我的问题。我已经明白并解决了这个问题。我为每行添加了标题,并删除了数据返回空的标题。 现在,它在我的应用程序中运行得非常好。我再一次感谢你们。

0

您可以将标题设为节标题而不是单元格的一部分,将事件和时间设置为表格单元格。否则,您将需要为自定义单元扩展UITableViewCell。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]; 
    dateView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"transactionHeader.png"]]; 

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 30)]; 
    headerLabel.tag = 100; 
    headerLabel.backgroundColor = [UIColor clearColor]; 
    headerLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
    headerLabel.font = [UIFont boldSystemFontOfSize:16]; 
    headerLabel.highlightedTextColor = [UIColor whiteColor]; 
    [dateView addSubview: headerLabel]; 
    return dateView; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     ... 
    } 
    ... 
    cell.textLabel.text = @"event1"; 
    cell.detailTextLabel.text = @"time1"; 
    return cell; 
} 
相关问题