1

我想设计一个表格视图,使每个单元格根据条件具有不同的背景图像。单元格背景有 10 个图像,我想加载它们,以便在单元格上重复图像模式。例如,单元格 1-10 分别从 1-10 获取图像。然后细胞 11-20 也拍摄图像 1-10,依此类推。我怎样才能做到这一点?

4

4 回答 4

3

给图像名称,如: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;

}

也许它会起作用。

于 2013-10-22T06:56:09.750 回答
3

首先用 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]];
}
于 2013-10-22T07:02:29.660 回答
2

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

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

比你可以在你想要的地方使用这个 imageName 。

于 2013-10-22T06:43:55.970 回答
1
- (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"];
    }
}
于 2013-10-22T06:46:09.730 回答