在UIlabel上面的图像?

时间:2013-06-19 18:09:02

标签: objective-c uitableview uilabel addsubview

我正在尝试在图片上方添​​加title3并将其添加到左侧的黑框中。

比如这个

enter image description here

我不确定为什么它不起作用但是我会非常感谢任何帮助。我对Objective-C很新,所以如果你看到我做错了什么或者做得更好,请不要犹豫告诉我。

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

    static NSString *CellIdentifier = @"Cell";
    ChanelFeeds *currentFeed = [[xmlParser feeds] objectAtIndex:indexPath.row];

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

        CGRect imageFrame = CGRectMake(0, 0, 120, 90);
        self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
        [cell.contentView addSubview:self.customImage];
        [self.customImage release];


        // Title
        CGRect contentFrame = CGRectMake(122, 2, 198, 45);
        UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
        title.tag = 0011;
        title.numberOfLines = 2;
        title.backgroundColor = [UIColor clearColor];
        title.textColor = [UIColor whiteColor];
        title.font = [UIFont boldSystemFontOfSize:14];
        [cell.contentView addSubview:title];
        [title release];

        // Views Placement
        CGRect contentFrame2 = CGRectMake(127, 70, 180, 15);
        UILabel *title2 = [[UILabel alloc] initWithFrame:contentFrame2];
        title2.tag = 0012;
        title2.numberOfLines = 1;
        title2.backgroundColor = [UIColor clearColor];
        title2.textColor = [UIColor whiteColor];
        title2.font = [UIFont italicSystemFontOfSize:14];
        [cell.contentView addSubview:title2];
        [title2 release];



        //Here's where the fails come in

        NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSURL *imgURL = [currentFeed thumbnailURL];
        NSArray *parts = [[NSString stringWithFormat:@"%@", imgURL] componentsSeparatedByString:@"/"];
        NSString *imageName = [parts objectAtIndex:[parts count]-2];

        NSString *filePath = [directoryPath stringByAppendingPathComponent:imageName];

        UIImage  *myview = [UIImage imageWithContentsOfFile:filePath];
        if(myview){
            cell.imageView.image = myview;

            //Add Black fucking border and stuff here!!!
        }else{


            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSData* data = [NSData dataWithContentsOfURL:[currentFeed thumbnailURL]];
                UIImage *thumbnail = [UIImage imageWithData:data];
                cell.imageView.image = thumbnail;
                [self.feedsTableView beginUpdates];
                [self.feedsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
                                           withRowAnimation:UITableViewRowAnimationNone];
                [self.feedsTableView endUpdates];


            });
        }


        CGRect contentFrame6 = CGRectMake(0, 67, 35, 13);
        UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
        imv5.alpha = 1;
        imv5.tag = 0014;
        [cell.contentView addSubview:imv5];
        imv5.image = [UIImage imageNamed:@"blackBorder.png"];
        imv5.backgroundColor = [UIColor clearColor];

        // Formatting views
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle];  NSNumber * formattedViews = [f numberFromString
                                                                                                                                                  :[currentFeed views]]; [f release]; NSNumber *firstNumber = formattedViews; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];[formatter setNumberStyle: NSNumberFormatterDecimalStyle]; convertedNumber = [formatter stringForObjectValue:firstNumber];


        // Basic stuff


        title.text = [currentFeed title];
        title2.text = [NSString stringWithFormat:@"%@ views", convertedNumber];


        totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
        NSString *word = @":00:";
        NSString *word2 = @"00:00:";
        if ([totalTime rangeOfString:word].location == NSNotFound || [totalTime rangeOfString:word2].location != NSNotFound) {
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"00:00:" withString:@"0:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"00:" withString:@""];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"01:" withString:@"1:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"02:" withString:@"2:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"03:" withString:@"3:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"04:" withString:@"4:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"05:" withString:@"5:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"06:" withString:@"6:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"07:" withString:@"7:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"08:" withString:@"8:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"09:" withString:@"9:"];
        }

        CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
        UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
        title3.backgroundColor = [UIColor clearColor];
        title3.textColor = [UIColor whiteColor];
        title3.tag = 0013;

        if([totalTime length] >= 5) {

            CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
            UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
            title3.tag = 0013;
            title3 = [[UILabel alloc] initWithFrame:contentFrame7];
            title3.backgroundColor = [UIColor clearColor];
            title3.textColor = [UIColor whiteColor];
            title3.font = [UIFont boldSystemFontOfSize:11];
            title3.text = totalTime;
            [title3 release];
        } else {
            CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
            UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
            title3 = [[UILabel alloc] initWithFrame:contentFrame7];
            title3.tag = 0013;
            title3.backgroundColor = [UIColor clearColor];
            title3.textColor = [UIColor whiteColor];
            title3.font = [UIFont boldSystemFontOfSize:11];
            title3.text = totalTime;
        }
        title3.numberOfLines = 1;
        [cell.contentView addSubview:title3];
        [title3 release];




    } else {
        //Update while scrolling

        NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle];  NSNumber * formattedViews = [f numberFromString
                                                                                                                                                  :[currentFeed views]]; [f release]; NSNumber *firstNumber = formattedViews; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];[formatter setNumberStyle: NSNumberFormatterDecimalStyle]; convertedNumber = [formatter stringForObjectValue:firstNumber];









        // views and titles
        UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
        UILabel *title2 = (UILabel *)[cell.contentView viewWithTag:0012];

        title.text = [currentFeed title];
        title2.text = [NSString stringWithFormat:@"%@ views", convertedNumber];









       totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
        NSString *word = @":00:";
        NSString *word2 = @"00:00:";
        if ([totalTime rangeOfString:word].location == NSNotFound || [totalTime rangeOfString:word2].location != NSNotFound) {
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"00:00:" withString:@"0:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"00:" withString:@""];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"01:" withString:@"1:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"02:" withString:@"2:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"03:" withString:@"3:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"04:" withString:@"4:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"05:" withString:@"5:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"06:" withString:@"6:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"07:" withString:@"7:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"08:" withString:@"8:"];
            totalTime = [totalTime stringByReplacingOccurrencesOfString:@"09:" withString:@"9:"];
        }
        UILabel *title3 = (UILabel *)[cell.contentView viewWithTag:0013];
        if([totalTime length] >= 5) {
            CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
            title3 = [[UILabel alloc] initWithFrame:contentFrame7];
            title3.textColor = [UIColor whiteColor];
            title3.backgroundColor = [UIColor clearColor];
            title3.text = totalTime;
        } else {
            CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
            title3 = [[UILabel alloc] initWithFrame:contentFrame7];
            title3.textColor = [UIColor whiteColor];
            title3.backgroundColor = [UIColor clearColor];
            title3.text = totalTime;
        }
        NSLog(@"%@ CHANGED TO : %@", [self timeFormatted:([currentFeed duration].intValue)-1], totalTime);
    }





    return cell;
}

1 个答案:

答案 0 :(得分:0)

我不确定它是否是此特定问题的根源,但您正在尝试从后台线程更新UI,这是不允许的。

在后台加载缩略图后,需要将UI更新调用封送回主线程。通过对主队列(dispatch_get_main_queue())的另一次调度来执行此操作。

相关问题