Iphone 类似gridView的UITableView

Iphone 类似gridView的UITableView,iphone,ios,objective-c,uitableview,uicollectionview,Iphone,Ios,Objective C,Uitableview,Uicollectionview,我有一个NSMutableArray,我在其中存储从服务url获取的项目并完成解析。我在UITableView中显示数组项目,就像gridview4列一样,每行的行数取决于数组计数。这类似于显示一周内所有可用的在线考试时间。因此,我必须逐个加载每天的计时,如果全部加载的话插槽是为特定的一天保留的,然后应显示该天的消息“无可用插槽”,然后在tableView中加载下一个数组项。但我只能得到最后一天的空位 我必须采用以下格式: 10/1/2013 10:00 AM 11:00 AM 12

我有一个NSMutableArray,我在其中存储从服务url获取的项目并完成解析。我在UITableView中显示数组项目,就像gridview4列一样,每行的行数取决于数组计数。这类似于显示一周内所有可用的在线考试时间。因此,我必须逐个加载每天的计时,如果全部加载的话插槽是为特定的一天保留的,然后应显示该天的消息“无可用插槽”,然后在tableView中加载下一个数组项。但我只能得到最后一天的空位

我必须采用以下格式:

10/1/2013

10:00 AM    11:00 AM   12:00 PM   1:00 PM
2:00 PM

11/1/2013

11:00 AM     12:00 PM   1:00 PM    2:00 PM 

12/1/2013

   No slot available

13/1/2013

10:00 AM    11:00 AM   12:00 PM   1:00 PM
2:00 PM     3:00 PM

............
到目前为止,我使用的是同一个tableview,并分别加载某一天的每个数组项,但我只能得到最后一天的计时

我怎样才能得到这种特殊的格式

任何关于使用哪个控件或如何使用同一tableView的建议/建议都是值得注意的

编辑:


这是我编写的代码。

首先,最好使用它来表示网格,而不是UITableView。它干净多了

接下来,在您的特定情况下,因为您试图在一行中显示4个网格,所以需要在UItableView的cellForRow委托中使用modulo%运算符。同时将uitableview委托中的项目数调整为totalitems/4。我敢打赌还有几件事我忘了提


我强烈建议您花点时间迁移到UICollectionView它的cleaner…

首先,您最好使用它来表示网格,而不是UITableView。它干净多了

接下来,在您的特定情况下,因为您试图在一行中显示4个网格,所以需要在UItableView的cellForRow委托中使用modulo%运算符。同时将uitableview委托中的项目数调整为totalitems/4。我敢打赌还有几件事我忘了提


我强烈建议您花点时间迁移到UICollectionView它的cleaner…

试试下面代码中的一些代码。。。删除其他一些对您不有用的代码

我有这种类型的需求,我用下面的代码和逻辑来完成它。。。也看到图像我想你想要这样的东西

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSArray* items = [_grids objectAtIndex:indexPath.section];
    
    //number of rows in one grid if one row will have 4 items
    //    float totalRow = ceilf(items.count/4);
    int total = [items count];
    
    float f = (float)total /4;
    int numberOfGridRows = ceilf(f);
    NSLog(@"\n\n Rows :>> %d",numberOfGridRows);
    //height of table row
    return numberOfGridRows * 36.0 + 120;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"Grid %d", section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString* cellIdentifier = @"gridCell";

    UITableViewCell *gridCell = [tableView dequeueReusableCellWithIdentifier:nil];
    
    if(gridCell == nil)        
    {
        gridCell =  [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        
        gridCell.accessoryType = UITableViewCellAccessoryNone;
        
        gridCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
//    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lion.jpg"]];
//    [img setFrame:CGRectMake(0, 0, 320, 120)];
//    [gridCell.contentView addSubview:img];
    
    UIImageView *imgUser = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"noImage.png"]];
    [imgUser setFrame:CGRectMake(5, 5, 100, 115)];
    [gridCell.contentView addSubview:imgUser];
    
    UILabel *lbl = [[UILabel alloc]init];
    lbl.text = @"Dr. P.R. Joshi";
    lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    [lbl setFrame:CGRectMake(110, 5, 200, 31)];    
    [gridCell.contentView addSubview:lbl];

    UILabel *lblAddress = [[UILabel alloc]init];
    [lblAddress setFrame:CGRectMake(110, 31, 200, 31)];        
    lblAddress.text = @"706,Atlanta Tower,Gulbai Tekra,Ahemdabad-007.";
    lblAddress.lineBreakMode = UILineBreakModeWordWrap;
    lblAddress.numberOfLines = 0;
    lblAddress.font = [UIFont fontWithName:@"Helvetica" size:12];
//    CGSize sizeToMakeLabel = [lblAddress.text sizeWithFont:lblAddress.font]; 
    lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y, 
                             200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] ); 

    lblAddress.textColor = [UIColor darkGrayColor];

    [gridCell.contentView addSubview:lblAddress];
    
    DYRateView *rateView = [[[DYRateView alloc] initWithFrame:CGRectMake(110, lblAddress.frame.origin.y + lblAddress.frame.size.height + 5, 160, 14)] autorelease];
    rateView.rate = 4.5;
    rateView.alignment = RateViewAlignmentLeft;
    [gridCell.contentView addSubview:rateView];
    
    UIImageView *imgArrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"left-arrow-right-md.png"]];
    [imgArrow setFrame:CGRectMake(300, 57, 12, 18)];
    [gridCell.contentView addSubview:imgArrow];
    
    NSArray* items = [_grids  objectAtIndex:indexPath.section];
    
    int imageIndex = 0;
    
    int yOffset = 4;
    
    while (imageIndex < items.count)        
    {
        int yPos =  7 + yOffset * 30;
        
        for(int i = 0; i < 4; ++i)
        {
            CGRect  rect = CGRectMake((0 + i * 80), yPos, 80, 31);
            
            if (imageIndex < [items  count]) {
                
                UIImageView* imageView = [self gridImageRect:rect forImage:[items objectAtIndex:imageIndex]];
                
                [gridCell.contentView addSubview:imageView];
                [imageView release];
                
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = rect;
                button.tag = imageIndex;
                [button addTarget:self 
                           action:@selector(btnTemp_Clicked:)
                 forControlEvents:UIControlEventTouchDown];
                
                [button setTitle:[NSString stringWithFormat:@"Title %d%d%d",indexPath.section,indexPath.row,imageIndex] forState:UIControlStateNormal];
                [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];

//                [button.titleLabel setTextColor:[UIColor blueColor]];
                [button setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"circle03.jpg"]] forState:UIControlStateNormal ];                
                [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];
                
                [button setNeedsDisplay];
                
                [gridCell.contentView addSubview:button];
                
                //            rect = CGRectMake(((80 * i)-4), (yPos+44), 80, 12);
                //            
                //            
                //            
                //            UILabel* label = [self descriptionOfImage:imageIndex inRect:rect];
                //            
                //            [gridCell.contentView addSubview:label];
                //            
                //            [label release];
            }
            ++imageIndex;
        }
        ++yOffset;
    }
    return gridCell;
}

-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
    [text retain];
    [withFont retain];
    CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
    
    [text release];
    [withFont release];
    
    return suggestedSize.height;
}

-(IBAction)btnTemp_Clicked:(id)sender{
    UIButton *btn = (UIButton *)sender;
    NSLog(@"Selected Image : %d  Button Title is ==>> %@",btn.tag,btn.titleLabel.text);
}

-(UIImageView *)gridImageRect:(CGRect)rect  forImage:(UIImage *)image
{
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:rect];
    imageView.image = image;
    
    return imageView;
}

-(UILabel *)descriptionOfImage:(int)imageNumber inRect:(CGRect)rect
{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    
    label.text = [NSString stringWithFormat:@"Image  %d", imageNumber + 1];
    
    label.textColor = [UIColor blackColor];
    
    label.backgroundColor = [UIColor clearColor];
    
    label.textAlignment = UITextAlignmentCenter;
    
    label.font = [UIFont fontWithName:@"ArialMT" size:12];
    
    return label;
}
查看此代码输出的屏幕截图

还可以查看以下UIGridView演示和教程链接

我希望这对你有帮助


请尝试我的以下代码中的一些代码。。。删除其他一些对您不有用的代码

我有这种类型的需求,我用下面的代码和逻辑来完成它。。。也看到图像我想你想要这样的东西

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSArray* items = [_grids objectAtIndex:indexPath.section];
    
    //number of rows in one grid if one row will have 4 items
    //    float totalRow = ceilf(items.count/4);
    int total = [items count];
    
    float f = (float)total /4;
    int numberOfGridRows = ceilf(f);
    NSLog(@"\n\n Rows :>> %d",numberOfGridRows);
    //height of table row
    return numberOfGridRows * 36.0 + 120;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"Grid %d", section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString* cellIdentifier = @"gridCell";

    UITableViewCell *gridCell = [tableView dequeueReusableCellWithIdentifier:nil];
    
    if(gridCell == nil)        
    {
        gridCell =  [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        
        gridCell.accessoryType = UITableViewCellAccessoryNone;
        
        gridCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
//    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lion.jpg"]];
//    [img setFrame:CGRectMake(0, 0, 320, 120)];
//    [gridCell.contentView addSubview:img];
    
    UIImageView *imgUser = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"noImage.png"]];
    [imgUser setFrame:CGRectMake(5, 5, 100, 115)];
    [gridCell.contentView addSubview:imgUser];
    
    UILabel *lbl = [[UILabel alloc]init];
    lbl.text = @"Dr. P.R. Joshi";
    lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    [lbl setFrame:CGRectMake(110, 5, 200, 31)];    
    [gridCell.contentView addSubview:lbl];

    UILabel *lblAddress = [[UILabel alloc]init];
    [lblAddress setFrame:CGRectMake(110, 31, 200, 31)];        
    lblAddress.text = @"706,Atlanta Tower,Gulbai Tekra,Ahemdabad-007.";
    lblAddress.lineBreakMode = UILineBreakModeWordWrap;
    lblAddress.numberOfLines = 0;
    lblAddress.font = [UIFont fontWithName:@"Helvetica" size:12];
//    CGSize sizeToMakeLabel = [lblAddress.text sizeWithFont:lblAddress.font]; 
    lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y, 
                             200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] ); 

    lblAddress.textColor = [UIColor darkGrayColor];

    [gridCell.contentView addSubview:lblAddress];
    
    DYRateView *rateView = [[[DYRateView alloc] initWithFrame:CGRectMake(110, lblAddress.frame.origin.y + lblAddress.frame.size.height + 5, 160, 14)] autorelease];
    rateView.rate = 4.5;
    rateView.alignment = RateViewAlignmentLeft;
    [gridCell.contentView addSubview:rateView];
    
    UIImageView *imgArrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"left-arrow-right-md.png"]];
    [imgArrow setFrame:CGRectMake(300, 57, 12, 18)];
    [gridCell.contentView addSubview:imgArrow];
    
    NSArray* items = [_grids  objectAtIndex:indexPath.section];
    
    int imageIndex = 0;
    
    int yOffset = 4;
    
    while (imageIndex < items.count)        
    {
        int yPos =  7 + yOffset * 30;
        
        for(int i = 0; i < 4; ++i)
        {
            CGRect  rect = CGRectMake((0 + i * 80), yPos, 80, 31);
            
            if (imageIndex < [items  count]) {
                
                UIImageView* imageView = [self gridImageRect:rect forImage:[items objectAtIndex:imageIndex]];
                
                [gridCell.contentView addSubview:imageView];
                [imageView release];
                
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = rect;
                button.tag = imageIndex;
                [button addTarget:self 
                           action:@selector(btnTemp_Clicked:)
                 forControlEvents:UIControlEventTouchDown];
                
                [button setTitle:[NSString stringWithFormat:@"Title %d%d%d",indexPath.section,indexPath.row,imageIndex] forState:UIControlStateNormal];
                [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];

//                [button.titleLabel setTextColor:[UIColor blueColor]];
                [button setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"circle03.jpg"]] forState:UIControlStateNormal ];                
                [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];
                
                [button setNeedsDisplay];
                
                [gridCell.contentView addSubview:button];
                
                //            rect = CGRectMake(((80 * i)-4), (yPos+44), 80, 12);
                //            
                //            
                //            
                //            UILabel* label = [self descriptionOfImage:imageIndex inRect:rect];
                //            
                //            [gridCell.contentView addSubview:label];
                //            
                //            [label release];
            }
            ++imageIndex;
        }
        ++yOffset;
    }
    return gridCell;
}

-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
    [text retain];
    [withFont retain];
    CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
    
    [text release];
    [withFont release];
    
    return suggestedSize.height;
}

-(IBAction)btnTemp_Clicked:(id)sender{
    UIButton *btn = (UIButton *)sender;
    NSLog(@"Selected Image : %d  Button Title is ==>> %@",btn.tag,btn.titleLabel.text);
}

-(UIImageView *)gridImageRect:(CGRect)rect  forImage:(UIImage *)image
{
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:rect];
    imageView.image = image;
    
    return imageView;
}

-(UILabel *)descriptionOfImage:(int)imageNumber inRect:(CGRect)rect
{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    
    label.text = [NSString stringWithFormat:@"Image  %d", imageNumber + 1];
    
    label.textColor = [UIColor blackColor];
    
    label.backgroundColor = [UIColor clearColor];
    
    label.textAlignment = UITextAlignmentCenter;
    
    label.font = [UIFont fontWithName:@"ArialMT" size:12];
    
    return label;
}
查看此代码输出的屏幕截图

还可以查看以下UIGridView演示和教程链接

我希望这对你有帮助

我想这对你有帮助

问候 萨阿德

我想这会对你有所帮助

问候
Saad

你能把你写的代码放进去吗。这样我们可以试着纠正错误。你能把你写的代码放进去吗。因此,我们可以尝试纠正错误。嗨,我想知道复制粘贴所有代码在这里有什么意义。包括你的逻辑和一切。这实际上会让用户感到困惑。我同意提供你所拥有的一切总是更好的。所以用户可以决定带什么和不带什么。但我认为@arizah已经成功地展示了表格视图。他实际上是在与我的逻辑作斗争。当时如果你提供这么长的代码。我不明白这一点。我很感激你也给了他截图,这将有助于他。但是我不同意你把所有的代码都贴在CellforRow的索引路径上,从宽度计算文本的重量,点击按钮,gridImageRect,descriptionOfImage等等。但是他到底想要什么,伙计???@ParasJoshi他想要什么我们需要问他。但我他妈的肯定他不想从宽度计算文本的重量,btnTemp\u点击,gridImageRect,DescriptionofImages:p@Rushi哦,好吧,伙计,如果我明白阿里什到底想要什么,那么我就把那个帖子删除了。。谢谢嗨,我想知道在这里复制粘贴你所有的代码有什么意义。包括你的逻辑和一切。这实际上会让用户感到困惑。我同意提供你所拥有的一切总是更好的。所以用户可以决定带什么和不带什么。但我认为@arizah已经成功地展示了表格视图。他实际上是在与我的逻辑作斗争。当时如果你提供这么长的代码。我不明白这一点。我很感激你也给了他截图,这将有助于他。但是我不同意你把所有的代码都贴在CellforRow的索引路径上,从宽度计算文本的重量,点击按钮,gridImageRect,descriptionOfImage等等。但是他到底想要什么,伙计???@ParasJoshi他想要什么我们需要问他。但我他妈的肯定他不想从宽度计算文本的重量,btnTemp\u点击,gridImageRect,DescriptionofImages:p@Rushi哦,好吧,伙计,如果我明白阿里什到底想要什么,那么我就离开 我喜欢那个邮递员。。谢谢