0

3 つのカスタム ボタンとタグを持つ tableView を作成しようとしています。3 つのボタンの 4 行に相当する 12 の画像があります。コードはすべての行に最初の 3 つの画像を表示するようになりました。12 個のボタンすべてに 12 個の画像すべてが必要です。

したがって、各ボタンには、他のボタンとは異なる画像とタグが必要です。

これを作成するのを手伝ってくれる人がいますか、これまでの私のコード は書式設定のために編集されています

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

static NSString *CellIdentifier = @"Cell";


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

}


btnImages = [[NSMutableArray alloc] initWithObjects:@"dog1.png",@"dog2.png",@"dog3.png",@"dog4.png",@"dog5.png",@"dog6.png"
             @"dog7.png",@"dog8.png",@"dog9.png",@"dog10.png",@"dog11.png",@"dog12.png",nil];

for (int i = 0; i < 3; i++) {
    //position the UiButtons in the scrollView
    CGRect frame = CGRectMake((i*100) + 35, 35, 45, 45);

    //Create the UIbuttons
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTag:i];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = frame;
    button.clipsToBounds = NO;
    button.contentMode = UIViewContentModeCenter;


    //Add images to the button
    UIImage *btnImg = [UIImage imageNamed:[btnImages objectAtIndex:i]];
    [button setBackgroundImage:btnImg forState:UIControlStateNormal];
     [cell.contentView addSubview:button];   
}

return cell;
}
4

1 に答える 1

0

列を置くだけ

btnImages = [[NSMutableArray alloc] initWithObjects:@"dog1.png",@"dog2.png",@"dog3.png",@"dog4.png",@"dog5.png",@"dog6.png"
         @"dog7.png",@"dog8.png",@"dog9.png",@"dog10.png",@"dog11.png",@"dog12.png",nil];

メソッド内viewDidLoad:

于 2012-04-23T10:16:58.453 に答える