在表视图中向单元格添加图像 [英] Adding images to a cell in table view

查看:130
本文介绍了在表视图中向单元格添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,其中我想显示表视图和表视图的每个单元格将显示5个不同的图像。让我说详细。我有10个图像,我想在表视图中显示它们.say 4图像在表视图的第一行然后下4在下一行,然后剩下的两个在下一行。一个新手,我无法得到一个启动。请帮助。任何帮助将赞赏。



感谢您,
Christy

解决方案



如何显示UITableView以编程方式?



那么你可以看看这个



http://www.iphonesdkarticles.com/2009/02/uitableview-adding- subviews-to-cells.html



如果你知道如何加载一个合适的视图,我认为你可以开始使用复杂的部分。

您需要在cellForRow方法中添加这行代码



您需要根据以下内容设置框架大小和图像名称:您的需求

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
if(indexPath.row == 0)
{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image1.png,]];
[cell.contentView addSubview:imageView];
[imageView release];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image2.png,]];
[cell.contentView addSubview:imageView];
[imageView release];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image3.png,]];
[cell.contentView addSubview:imageView];
[imageView release];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image4.png,]];
[cell.contentView addSubview:imageView];
[imageView release];
}
else if(indexPath.row == 1)
{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15) ;
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image5.png,]];
[cell.contentView addSubview:imageView];
[imageView release];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image6.png,]];
[cell.contentView addSubview:imageView];
[imageView release];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image7.png,]];
[cell.contentView addSubview:imageView];
[imageView release];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image8.png,]];
[cell.contentView addSubview:imageView];
[imageView release];
}
else if(indexPath.row == 2)
{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15) ;
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image9.png,]];
[cell.contentView addSubview:imageView];
[imageView release];

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14,14,15,15)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@image10.png,]];
[cell.contentView addSubview:imageView];
[imageView release];
}
else
{
//这里做一些事情
}
return cell;
}

编辑:



@Christy你可以有一个按钮,可以使表视图滚动。但是iphone用户习惯于在表格右侧看到滚动条,看看表格是否有更多的数据。



但是如果你想有一个用于滚动表格的按钮是代码



在此块中的tableView的cellForRow方法中 else if(indexPath.row == 2) {} 使用向下箭头添加UIButton,如下所示

  UIButton * button = [[UIButton alloc ] initWithFrame:CGRectMake(220.0f,5.0f,150.0f,38.0f)]; 
[button setImage:[UIImage imageNamed:@Arrow] forState:UIControlStateNormal];
[button addTarget:self action:@selector(doSomething)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
[button release];

和选择器方法

   - (void)doSomething 
{
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]
}


I am creating an app in which i want to display a table view and each cell of table view will be displaying 5 different images.Let me say in detail.I have 10 images and i want to show them in a table view .say 4 images in first row of table view then next 4 in next row ,then the remaining two in next row.Being a newbie i am unable to get a startup.Please help.Any help will be appreciated.

Thanks, Christy

解决方案

I think for startup you can go over here

How to display the UITableView programmatically?

then you can have a look at this

http://www.iphonesdkarticles.com/2009/02/uitableview-adding-subviews-to-cells.html

Ok if you know how to load a uitableview than I think you can get started with complex part.

You need to add this line of code in your cellForRow method

You need to set the frame size and image name according to your requirement

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
    }
    if(indexPath.row == 0)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image1.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image2.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image3.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image4.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    else if(indexPath.row == 1)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image5.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image6.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image7.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image8.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    else if(indexPath.row == 2)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image9.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 15, 15)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image10.png", ]];
        [cell.contentView addSubview:imageView];
        [imageView release];        
    }
    else
    {
        // Do something here
    }
    return cell;
}

Edit:

@Christy you can have a button that can make a table view scroll. But iphone users are used to see the scroll bar on the right of the table to see if the table has any more data or not.

But if you want to have a Button that scrolls the table than here is the code

In your cellForRow method of tableView in this block else if(indexPath.row == 2){} add a UIButton with a down arrow like this

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220.0f, 5.0f, 150.0f, 38.0f)];
[button setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
[button release];

and in the selector method

- (void)doSomething
{
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]
}

这篇关于在表视图中向单元格添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆