我想将值设置为按钮标记为1或0,这些值来自表行中的数据库。 当值为1时,我希望该按钮图像为star.png,如果值为0,我希望按钮图像为dot.png。
答案 0 :(得分:0)
什么是你的问题spikydude。
在我看来,你想在tableView的单元格上添加一个按钮,该图像将是星形或点形。并且您可以轻松地从数据库获取。只需在if
方法中添加一个cellForRowAtIndexPath
条件即可获得所需内容/
编辑:
- (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] autorelease];
}
if(value==0)
{
//create your button with proper frame
yourButton.tag = value;
[yourButton setImage:[UIImage iamgeNamed:@"dot.png"];
}
else if(value==1)
{
//create your button with proper frame
yourButton.tag = value;
[yourButton setImage:[UIImage iamgeNamed:@"star.png"];
}
else
{
}
return cell;
}
或者,如果您想要如何从数据库中检索值,请指定该值。
答案 1 :(得分:0)
如果您有两张以上的图片,可以使用以下方法:
NSArray * picturesName = [NSArray arrayWithObjects:@"picture0.png",@"picture1.png", ... , nil];
UIImage * img;
if (value < [picturesName count]) {
img = [UIImage imageNamed:[picturesName objectAtIndex:value]];
} else {
img = [UIImage imageNamed:@"default.png"]
}
然后你可以按下setImage
按钮