在ios中重新加载tableview时无法显示正确的图像
我有一个问题:我想重新加载tableview,但它只重新加载标签,我不能重新加载按钮的图像。这是我的密码:在ios中重新加载tableview时无法显示正确的图像,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个问题:我想重新加载tableview,但它只重新加载标签,我不能重新加载按钮的图像。这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [_tabl
- (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] autorelease];
UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.tag = 1000;
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
NSLog(@"AllItemsArrayarray: %@", AllItemsArray);
// FileNameLabel.text =[temp objectAtIndex:indexPath.row];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 150, 25)];
UploadTimeLabel.tag = 2000;
UploadTimeLabel.backgroundColor = [UIColor clearColor];
UploadTimeLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
UploadTimeLabel.textColor = [UIColor grayColor];
// UploadTimeLabel.text = [UploadTimeArray objectAtIndex:indexPath.row];
[cell.contentView addSubview: UploadTimeLabel];
[UploadTimeLabel release];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:@"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
market = [UIButton buttonWithType:UIButtonTypeCustom];
market.tag = 4000;
// [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
// [market setTag:indexPath.row];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
if([sellingArray count]>0)
{
NSLog(@"sellingArray %@",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
// auctionButton.enabled = NO;
}
}
[marketButton addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[marketButton setTag:indexPath.row];
//}
if( [temp count] > 0)
{
UILabel *fileNameLbl = (UILabel*)[cell.contentView viewWithTag:1000];
fileNameLbl.text =[temp objectAtIndex:indexPath.row];
UILabel *uploadlbl = (UILabel*)[cell.contentView viewWithTag:2000];
uploadlbl.text =[UploadTimeAllArr objectAtIndex:indexPath.row];
}
if([priceNewArray count]> 0)
{
UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
pricelbl.text =[NSString stringWithFormat:@"$%@",[priceNewArray objectAtIndex:indexPath.row]];
if ([sellingArray count]>0) {
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]){
pricelbl.hidden = NO;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]){
pricelbl.hidden = YES;
}
}
}
UIImage *image = [UIImage imageNamed:@"More.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame; // match the button's size with the image size
//[button setBackgroundImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
我使用了上面的代码,当调用[table reloaddata]
时,除了按钮(button market)之外,所有重新加载的标签都没有显示正确的图像。我不知道为什么。你能给我一些建议吗?提前感谢-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath始终遵循相同的模式。
如果找到一个单元格,它会将其出列,然后由您来配置它。如果找不到单元格(nil==单元格),则必须创建一个新单元格,然后执行配置步骤
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格=[\u tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
market=[UIButton按钮类型:UIButtonTypeCustom];
}
//配置市场按钮
返回单元;
}
一个一般建议是使用故事板/界面生成器来配置所需的单元格,或者使用子类UITableViewCell
来配置您自己的单元格。这将极大地简化您的代码并提高可读性。我可以看到,您在需要创建单元格时创建/配置市场按钮,但在使用排队的单元格时却不创建/配置市场按钮。@BerndRabe:我如何修复它?请帮帮我。谢谢,老师,我看不出你的if条款结束了。尝试删除除market按钮之外的所有内容,并在达到配置代码时进行调试。我在重新加载数据时进行调试,marketButton为0x0000。它不能初始化吗@伯恩德拉贝