【问题标题】:iOS tableview cell handlingiOS 表格视图单元格处理
【发布时间】:2011-04-29 08:05:39
【问题描述】:

我有这段代码,其中“lineaRedToday”是一个 UIImageView:

- (void)viewDidLoad {

[super viewDidLoad];

lineaRedToday = [UIImage imageNamed:@"line4.png"];}



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


static NSString *MyIdentifier = @"MyIdentifier";
MyIdentifier = @"tblCellView";

TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
    cell = tblCell;
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[[lineDay objectAtIndex:currentTodaylineRed -1] setImage:lineaRedToday];


return cell;



- (IBAction) change{

    lineaRedToday = [UIImage imageNamed:@"linea.png"];
    [lineDay setImage:lineaRedToday];
    [myTableView reloadData];
}

这是一个带有 15 个自定义 UITableViewCell 的 UITableView,我想在 UIImageView 上更改图像,这个 ImageView 是“lineDay”,lineDay 存在于所有单元格中,我想在所有单元格中更改其图像,但是 IBAction “更改”仅在最后一个单元格中更改 UIImageView 而根本不更改......为什么?

【问题讨论】:

    标签: objective-c cocoa-touch ios uitableview tableview


    【解决方案1】:

    是的,它会更改最后一个单元格中的图像,因为它只有最后一个单元格的引用,如果您想这样做,那么当您在 cellForRowAtIndexPath 中创建单元格时,请检查 BOOL 并根据该 BOOL 设置图像。同样在 IBAction 中更改该 BOOL 值并在表视图上调用 reloadData 以重新加载它。作为-

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
        {
            static NSString *CellIdentifier                 = @"Test";
    
            UITableViewCell *cellView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
            if (cellView == nil) 
            {
                cellView = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    
                UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 85.0f)];
                [bgImgView setTag:1];
                [bgImgView setBackgroundColor:[UIColor clearColor]];
                [cellView.contentView addSubview:bgImgView];
                [bgImgView release];
            }
    
            UIImageView *imgView = (UIImageView*)[cellView viewWithTag:1];
    
            if (buttonPressed) 
            {
                [imgView setImage:[UIImage imageNamed:@"listing_bg"]];
            }
            else 
            {
                [imgView setImage:[UIImage imageNamed:@"featured_listing_bg"]];
            }
            return cellView;
        }
    
    
    - (IBAction) change{
        buttonPressed = !buttonPressed;
        [myTableView reloadData];
    }
    

    【讨论】:

    • 我尝试使用我在 IBAction 中更改的布尔值,但它只更改了最后一个单元格
    【解决方案2】:

    首先,lineaRedToday 是 UIImage,而不是 UIImageView。第一个是图像,第二个是视图层次结构中的对象。

    然后,在你的代码中

    if(cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
        cell = tblCell;
    }
    

    您使用tblCell。我看不出这是什么。你应该从你刚刚从笔尖加载的东西中分配一些东西。

    我不知道lineDay 到底是什么,但单个视图(即UIImageView)只能出现在一个 单元格中,而不是多个。如果您更改图像 (lineaRedToday),您仍然需要在视图中设置此新图像。应该有类似

    cell.yourImageView.image = linaRedToday;
    

    配置单元时。

    【讨论】: