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

高さを指定できます。

テーブルの幅と 2 番目のメソッドの高さを使用して最初の UIView を割り当て/初期化してから、背景の UIImageView とタイトルのラベルなど、任意の数のビューを追加します。

于 2012-07-31T14:45:36.387 に答える
0

テーブルビューセクションの背景画像を変更することはできないと思います。このような要求を行いたい場合は、行のセルをセクションとして試してみてください。つまり、各行をテーブルのセクションとして扱い、その中に別のタグを持つテーブル ビューを 1 つ追加します。それはあなたの要求を満たします。または、各行のテイク

于 2013-01-21T04:51:39.750 に答える
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 に答える