0

是否可以更改uitableview中每个部分的背景图像?

我想为uitableview中的每个部分添加背景图片有人知道我该怎么做吗?

提前致谢!

喜欢这张图片 --> 分别为周三、周四和周五放置不同的背景图片

在此处输入图像描述

编辑我想为星期三添加图像 1 图像 2 为星期四图像 3 为星期五和.....我该如何指定?

编辑

这是创建节标题的代码我也想拥有背景

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if(section == 0)
    return @"Monday";
else if(section == 1){
    return @"Tuesday";
}else if(section == 2){
    return @"Wednesday";
} else if(section == 3){
    return @"Thuesday";
} else if(section == 4){
    return @"Friday";
} else if(section == 5){
    return @"Saturday";
}else
    return @"Sunday";

}
4

4 回答 4

2

您可以根据 indexPath 在 cellForRowAtIndexPath 委托方法中更改背景,如下所示:

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

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    int maxRow = 3;

    if (cell == nil)
    {
        cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]]];
    }
    else
    {
        UIImageView *background = (UIImageView *)cell.backgroundView;
        background.image = [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]];
    }

    return cell;
}

// To change header backgrounds

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    int maxRow = 3;

    UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(section, maxRow)]]];

    return headerView;
}

然后,您只需创建图像,为所需数量的标题/行编号,即。background_image0.png、background_image1.png、background_image2.png、...等等。MIN 将根据您决定的最大背景限制金额。希望有帮助。

编辑:根据 Henri 的评论更改了 cellForRowAtIndexPath。我忽略了,谢谢!这是为了 ARC 兼容性。

于 2012-07-31T13:23:50.897 回答
1

您可以使用:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

为节标题指定任何类型的 UIView。这另一个委托方法:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

让您指定高度。

只需使用第二种方法中的表格宽度和高度在第一个中分配/初始化 UIView,然后向其中添加任意数量的视图,例如作为背景的 UIImageView,然后是标题的标签。

于 2012-07-31T14:45:36.387 回答
0

ezekielDFM给出的迭代(并且主要是纠正)示例。请注意,此代码与 ARC 不兼容(之前的示例可能是这样)。

// To change cell backgrounds
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TaskCellRow";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    int maxRow = 3;

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

        UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]]] autorelease];

        cell.backgroundView = imageView;
    } else {
        // Reusing, we need to swap out the image of the background
        cell.backgroundView.image = [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]];
    }

    return cell;
}

// To change header backgrounds
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    int maxRow = 3;

    UIImageView *headerView = [[[UIImageView alloc] initWithImage:[NSString stringWithFormat:@"background_image%i.png", MIN(section, maxRow)]] autorelease];

    return headerView;
}
于 2012-07-31T13:40:40.837 回答
0

我猜不可能更改表格视图部分的背景图像。如果您想做这样的要求,请尝试将行的单元格作为部分。即,对于每一行,都将其视为表格的一部分,并在其中添加 1 个具有不同标签的表格视图。它会满足你的要求。否则对于每一行采取

于 2013-01-21T04:51:39.750 回答