【问题标题】:UItableView with multiple images and setting onclick on each one of them具有多个图像的 UItableView 并在每个图像上设置 onclick
【发布时间】:2012-05-05 01:42:38
【问题描述】:

我有一个场景,我想在 tableview 的行上保留图像的“缩略图”。我在运行时决定连续的图像数量。

其次,单击任何图像时,我想启动一个视图控制器,其中包含详细的图像和一些描述。

我怎样才能做到这一点?

|________________|
| 1 2 3  4  5  6 |
|________________|
| 7 8 9 10 11 12 |
|________________|
| 12 14 15       |
|________________|

把上面的数字想象成缩略图。单击任何数字我想启动一个新的视图控制器,它会提供有关图像的详细信息。

代码sn-p:

-(void) populateTableView
{

    NSMutableArray* srcArray = [[NSMutableArray alloc] initWithArray:[[ImageDataSource sharedImageDataSource] getThumbnailsSrcArray]];

    int noOfImages = srcArray.count;

    float rowc = (noOfImages / ROW_ELEM_COUNT) + 0.5;

    int rowCount = roundf(rowc);

    titleData = [[NSMutableArray alloc]init];

    int j=0;
    for (int i=0; i<rowCount; i++) {

        NSMutableArray* rowArray = [[NSMutableArray alloc] init];

        for (int k=0; k < ROW_ELEM_COUNT; k++) {

            if (j < noOfImages) {
                NSString* imgPath = [srcArray objectAtIndex:j];
                UIImage* img = [[UIImage alloc] initWithContentsOfFile:imgPath];
                [rowArray addObject:img];
                [img release];
                imgPath=nil;
            }
            j++;
        }
        [titleData addObject:rowArray]; 
    }

    titleView = [[UITableView alloc] initWithFrame:CGRectMake(90.0,156.0,590.0,630.0) 
                                             style:UITableViewStyleGrouped];
    titleView.delegate = self;

    [self.view addSubview:titleView];
    [titleView release];

}

所以基本上我有一个数组作为我的 DS。每个数组的索引将是表格视图的行,并且里面的数组将有图像。但我不确定如何填充表格视图。 有人知道吗?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    你有两个选择。

    • 添加带有 UIImage 的 UIButton,并将每个按钮添加为 tableview 单元格上的子视图。

    • 找出用户在单元格上点击的位置,并从此处计算他们点击的图像以及要采取的操作:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch* touch = [touches anyObject];
        // do your stuff
        //..
    }
    

    【讨论】:

    • 我是否必须继承 UITableViewCell ?内容类型只有图像(或 uibutton)所以如果我们在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中创建它们应该没问题
    • 是的,您可以通过此方法轻松地将子视图添加到cell.contentView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多