2012-10-17 58 views
0

我还没有在这里找到任何好的资源,所以我认为这将是一件很棒的事情,并且阻止我拉我的头发出。iOS - 在单个单元格内显示对象数组 - 重复

代理应用程序“议程”(日历应用程序) - 我有一个tableview与每个单元格代表一天。下一步是将eventsArray放置在适当的单元格内 - 通过cellForRowAtIndexPath。

我只熟悉显示具有节和行的eventsArray - 使用每个indexPath来表示一个事件...不是一个事件数组。 (下面我发布了其他人在该部门寻求帮助的工作代码,并作为参考)。

如果有人能够放置一些代码来执行单个单元格内的eventsArray会很好 - 我假设它是对我不熟悉的cellForRowAtIndexPath的使用。先谢谢你。任何问题只是拍摄。

{

#pragma mark - TableView stuff 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    A1Section *a1section = [self.sections objectAtIndex:section]; 

    return [a1section.rows count]; 

} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [self.sections count]; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    A1Section *a1section = [self.sections objectAtIndex:section]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"EEEE, MMM d, yyyy"]; 

    NSString *myDateString = [formatter stringFromDate:a1section.date]; 
    return [NSString stringWithFormat:@"%@", myDateString]; 
} 


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 47; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 75; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UILabel *timeLabel = nil; 
    UILabel *nameLabel = nil; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.backgroundColor = [UIColor redColor]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 



     timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 13.0, 55.0, 15.0)]; 
     [timeLabel setTag:1]; 
     [timeLabel setBackgroundColor:[UIColor clearColor]]; 
     timeLabel.textAlignment = UITextAlignmentLeft; 
     [timeLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]]; 
     [timeLabel setTextColor:[UIColor blackColor]]; 

     [timeLabel setLineBreakMode:NSLineBreakByClipping]; 
     [timeLabel setNumberOfLines:1]; 
     [cell.contentView addSubview:timeLabel]; 


     nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(85.0, 11.0, 200.0, 20.0)]; //x = 80.0, width = 235 
     [nameLabel setTag:2]; 
     [nameLabel setBackgroundColor:[UIColor clearColor]]; 
     nameLabel.textAlignment = UITextAlignmentLeft; 
     [nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:17]]; 
     [nameLabel setTextColor:[UIColor blackColor]]; 

     [nameLabel setLineBreakMode:NSLineBreakByTruncatingTail]; 
     [nameLabel setNumberOfLines:1]; 
     [cell.contentView addSubview:nameLabel]; 



    } else { 
     timeLabel = (UILabel *)[cell.contentView viewWithTag:1]; 
     nameLabel = (UILabel *)[cell.contentView viewWithTag:2]; 


    } 

    A1Section *a1section = [self.sections objectAtIndex:indexPath.section]; 
    PFObject *object = [a1section.rows objectAtIndex:indexPath.row]; 


    NSDate *someDate = [object objectForKey:@"startDate"]; 
    NSString *time = [self.dateFormatter stringFromDate:someDate]; 
    [(UILabel *)[cell.contentView viewWithTag:1] setText:time]; 

    [(UILabel *)[cell.contentView viewWithTag:2] setText:[object objectForKey:@"textContent"]]; 




    return cell; 

} 

}

+0

我不能在你的代码中看到任何eventsArray。什么是eventsArray? –

+0

也许你正在试图让单元响应它包含的控件,例如textField或按钮? –

回答

0

我不完全知道你是问什么,但我相信你会问如何在cellForRowAtIndexPath使用数组的内容进行显示。如果是这样,你只需要一个包含你想要的数据的数组属性。然后,在cellForRowAtIndexPath,你可以做类似

NSString *name = [self.dataArray objectAtIndex:indexPath.row]; 
nameLabel.text = name; 

这是否回答你的问题?

相关问题