iostableview单元格处理



我有这个代码,其中"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];
}

这是一个UITableView与15自定义UITableViewCell,我想改变图像在一个UIImageView,这个ImageView是"lineDay",lineDay是存在于所有单元格,我想改变它的图像在所有单元格,但IBAction"改变"改变UIImageView只在最后一个单元格,而不是在....为什么?

是的,它会改变最后一个单元格中的图像,因为它只有最后一个单元格的引用,如果你想这样做,那么当你在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];
}

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

然后,在代码

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

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

我不知道lineDay到底是什么,但是单个视图(即UIImageView)只能存在于一个单元格中,而不是很多。如果你改变了图像(lineaRedToday),你仍然需要在视图中设置这个新图像。应该有像

这样的东西
cell.yourImageView.image = linaRedToday;

最新更新