-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