gpt4 book ai didi

iphone - 当UIImage不存在时如何显示UILabel

转载 作者:行者123 更新时间:2023-11-29 03:45:15 24 4
gpt4 key购买 nike

我有一个 UITableView,我正在其中从服务器加载图像。但有时 UITableView 上没有图像可显示,此时我想显示 UILabel。想知道我将如何实现这一点。我将不胜感激任何帮助或代码片段来实现这一目标。非常感谢!

你说的我都试过了。当您加载表格时,第一次一切正常,但是一旦您开始滚动所有标签和按钮,就会到处都是。
这是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView                         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
if (msgImgFile){
NSLog (@"Image file found!");
lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 360, 200, 20)];
lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 378, 150, 20)];
lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 398, 150, 20)];
btnPlayStop.frame = CGRectMake(255.0f, 375.0f, 30.0f, 30.0f);
}
else
{
NSLog(@"Image file not found. Simply load the UILabel and UIButton");
lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 68, 150, 20)];
lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 88, 150, 20)];
btnPlayStop.frame = CGRectMake(255.0f, 45.0f, 30.0f, 30.0f);

}
lblOne.font = [UIFont fontWithName:@"Arial" size:12];
[lblOne setBackgroundColor:[UIColor clearColor]];
lblOne.tag = 1;


lblTwo.font = [UIFont fontWithName:@"Arial" size:12];
[lblTwo setBackgroundColor:[UIColor clearColor]];
lblTwo.tag = 2;

lblThree.font = [UIFont fontWithName:@"Arial" size:10];
[lblThree setBackgroundColor:[UIColor clearColor]];
lblThree.tag = 3;

lblFour = [[UILabel alloc] initWithFrame:CGRectMake(10, 24, 150, 20)];
lblFour.font = [UIFont fontWithName:@"Arial" size:12];
[lblFour setBackgroundColor:[UIColor clearColor]];
lblFour.tag = 4;

btnPlayStop = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPlayStop setTitle:@"Play" forState:UIControlStateNormal];
[btnPlayStop setImage:[UIImage imageNamed:@"Play Button.png"] forState:UIControlStateNormal];
[btnPlayStop addTarget:self action:@selector(playRecordClicked:) forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:lblOne];
[cell addSubview:lblTwo];
[cell addSubview:lblThree];
[cell addSubview:lblFour];
[cell.contentView addSubview:btnPlayStop];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{

msgObjImg = (PFObject *)[self.imageDataMutArray objectAtIndex:indexPath.row];
createdDt = msgObjImg.createdAt;


msgImgFile = [msgObjImg objectForKey:@"siqImage"];
NSData *imgData = [msgImgFile getData];

UIImage *msgImgFound = [UIImage imageWithData:imgData];
UIImage *newImg = [self scaleImage:msgImgFound toSize:CGSizeMake(280.0, 300.0)];

dispatch_sync(dispatch_get_main_queue(), ^{

UILabel *dtTimeLabel = (UILabel *)[cell viewWithTag:3];
NSDateFormatter *dtFormat = [[NSDateFormatter alloc]init];
[dtFormat setDateFormat:@"MM-dd-yyyy HH:mm"];
[dtFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-18000]];
NSString *createdDtString = [dtFormat stringFromDate:createdDt];
dtTimeLabel.text = [NSString stringWithFormat:@"Received on: %@",createdDtString];
[[cell imageView] setImage:newImg];
[cell setNeedsLayout];
}
return cell;

}

最佳答案

我可以看到问题出在哪里,并可以告诉您解决此问题的正确步骤。

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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];

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

lblOne = [[UILabel alloc]init];
lblOne.tag = 1;

lblTwo = [[UILabel alloc]init];
lblTwo.tag = 2;
lblThree = [[UILabel alloc]init];
lblThree.tag = 3;
lblFour = [[UILabel alloc]init];
lblFour.tag = 4;
btnPlayStop = [[UILabel alloc]init];
btnPlayStop.tag = 5;

// Perform addition functions ( your requirements )

[cell.contentView addSubview:lblOne];
[cell.contentView addSubview:lblTwo];
[cell.contentView addSubview:lblThree];
[cell.contentView addSubview:lblFour];
[cell.contentView addSubview:btnPlayStop];

}
else
{
lblOne = (UILabel*)[cell.contentView viewWithTag:1];
lblTwo = (UILabel*)[cell.contentView viewWithTag:2];
lblThree = (UILabel*)[cell.contentView viewWithTag:3];
lblFour = (UILabel*)[cell.contentView viewWithTag:4];
btnPlayStop = (UILabel*)[cell.contentView viewWithTag:5];

// AND SO ON ...
}

// SET THE VALUES HERE FOR YOUR CONTENT VALUES

return cell;
}

您需要使用动态内容 View 创建单元格。尝试上面的代码片段,并根据您自己的修改..

关于iphone - 当UIImage不存在时如何显示UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17837966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com