-2
大家好, 我是新的iOS开发。现在我正在处理自定义tableView,在这我使用自定义tableViewCell。在该单元格上,我在imageView上添加了文本标签。但它没有在图像视图上显示文字标签。只有图像显示。 如果有人对此问题有任何解决方法,请帮助我。我如何添加文本标签图像在ios中的自定义tableviewcell
这里是我的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";
ItemsCellVC *cell = (ItemsCellVC *)[TableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ItemsCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
if ([arrayForItemDictionary count]!=0) {
if (tableView == aSearchDisplayController.searchResultsTableView)
{
dictItemFromDatabase=[filteredListContent objectAtIndex:indexPath.row];
}
else
{
dictItemFromDatabase=[arrayForItemDictionary objectAtIndex:indexPath.row];
}
}
//cell.containerNameLabel.text=[dictItemFromDatabase valueForKey:@"PickerName"];
cell.itemNameLabel.text = [dictItemFromDatabase valueForKey:@"ItemNameTF"];
cell.containerImgView.image = [UIImage imageNamed:@"12-02 (1)"];
UIImage *image = [UIImage imageNamed:@"2-02 (1).png"];
cell.containerImgView.image = [self addText:image text:@"Hello there"];
return cell;
}
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
CGContextSelectFont(context, "Arial",20, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
CGContextShowTextAtPoint(context,10,10,text, strlen(text));
CGImageRef imgCombined = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
CGImageRelease(imgCombined);
return retImage;
}
添加cellforrowatindex代码 – 2014-10-16 12:56:22
可以显示你的代码 – 2014-10-16 12:59:40
最好的wa y是使用imageView和标签制作自定义单元格,并在单元格初始化时将它们添加到子视图中。 然后确保它们连接到一个属性,以便您可以配置框架以及内容。 – iYoung 2014-10-16 13:02:40