UITableViewCell带有3个按钮,带有图像和标签

时间:2012-04-23 09:51:42

标签: uitableview uibutton

我正在尝试创建一个包含3个自定义按钮和标签的tableView,我有12个图像,相当于4行3个按钮。现在代码现在显示所有行中的前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;
}

1 个答案:

答案 0 :(得分: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:

相关问题