将imageView添加到表视图的下一行

时间:2011-06-22 09:30:23

标签: objective-c xcode ipad uitableview

在我的应用程序中我有一个表视图,我将4个图像视图添加到我的表视图的第一行。现在我想将图像视图添加到下一行。如何在现有代码中执行此操作help.I我发布了我的部分代码: -

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

    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];

        UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
        UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
        UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
        UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
        UIImageView * imageView5 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
        UIImageView * imageView6 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];

        int j=0;
        imageView1.tag = j;
        imageView2.tag = j+1;
        imageView3.tag = j+2;
        imageView4.tag = j+3;
        imageView5.tag = j+4;
        imageView6.tag = j+5;

        [cell.contentView addSubview:imageView1];
        [cell.contentView addSubview:imageView2];
        [cell.contentView addSubview:imageView3];
        [cell.contentView addSubview:imageView4];
        [cell.contentView addSubview:imageView5];
        [cell.contentView addSubview:imageView6];
}

for ( int i = 1; i <= j; i++ ) {
    imageView = (UIImageView *)[cell.contentView viewWithTag:i];
    imageView.image = nil;
}

int photosInRow;

if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([sentence count] % 4 == 0) ) {
    photosInRow = 4;
} else {
    photosInRow = [sentence count] % 4;
}

for ( int i = 1; i <=[sentence count]; i++ ){
    imageView = (UIImageView *)[cell.contentView viewWithTag:i];
    [self setImage1:imageView];
}

    return cell;
}

我想mu imageView 5和6在下一行。请帮忙。怎么做?我正在撞墙挡住这个。

谢谢, 克里斯蒂

1 个答案:

答案 0 :(得分:1)

你可以尝试类似下面的内容...你基本上检查当前的行号然后构建你需要的逻辑......

UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) 
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
}

int j=0; imageView1.tag = j;


if(0 == [indexPath row])
{
UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];

UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];

UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];

UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];


imageView2.tag = j+1;

imageView3.tag = j+2;

imageView4.tag = j+3;

    [cell.contentView addSubview:imageView1];

            [cell.contentView addSubview:imageView2];

            [cell.contentView addSubview:imageView3];

            [cell.contentView addSubview:imageView4];



}else if(0 == [indexPath row])
{
UIImageView * imageView5 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];

UIImageView * imageView6 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];


imageView5.tag = j+1;

imageView6.tag = j+2;



            [cell.contentView addSubview: imageView5];

            [cell.contentView addSubview:imageView6];


}

    for ( int i = 1; i <= j; i++ ) {
    imageView = (UIImageView *)[cell.contentView viewWithTag:i];

    imageView.image = nil;

}