繁体   English   中英

ios应用程序具有不同背景图像的自定义tableview单元格

[英]custom tableview cells with different background images for ios application

我想设计一个tableview,以便每个单元根据条件具有不同的背景图像。 单元格背景有10张图像,我想加载它们,以便在单元格上重复图像图案。 例如,单元1-10分别从1-10拍摄图像。 然后单元格11-20也拍摄图像1-10,依此类推。 我该如何实现?

给出图像名称,例如:images_1 / 2 ... / 10.png

 //write it

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

  CustomCell  *Cell  = (CustomCell *)[tabeleYourTurn dequeueReusableCellWithIdentifier:CellIdentifier];
    if (Cell == nil)
    {
        NSArray *topLevelObject= [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        }

        for (id currentobject in topLevelObject)
        {
            if ([currentobject isKindOfClass:[UITableViewCell class]])
            {
                Cell = (CustomCell *) currentobject;

                break;
            }
        }
    }

 NSString *image = [NSString stringWithFormat:@"images_%d.png", indexPath.row % 10 + 1];
 Cell.imageView.image = [UIImage imageNamed:image];

return Cell;

}

可能会起作用。

首先使用ImageName创建一个数组。

喜欢

NSArray *ImageArray=[[NSArray alloc]initWithObjects:@"1.png",@"2.png"];

然后这个数组有10张图片

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];

}


// create a background image for the cell:
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setBackgroundView:bgView];
    [cell setIndentationWidth:0.0];


((UIImageView *)cell.backgroundView).image = [UIImage imageNamed:[ImageArray objectAtIndex:indexPath.row%10]];
}

假设您的图片名称为1、2 ... 10,则可以尝试执行以下操作:

NSString *imageName = [NSString stringWithFormat:@"%d", indexPath.row % 10 + 1];

比您可以在所需的位置使用此imageName。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];

    if( (indexPath.row % 10 ) <= 1)
    {
        // set your image
        cell.imageView.image = [UIImage imageNamed:@"image1.png"];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM