简体   繁体   English

iOS tableview 单元格处理

[英]iOS tableview cell handling

I have this code, where "lineaRedToday" is an UIImageView:我有这段代码,其中“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];
}

It is a UITableView with 15 custom UITableViewCell and I want to change the image at an UIImageView, this ImageView is "lineDay", lineDay is present in all cells, and I want change its image at all cells, but the IBAction "change" change the UIImageView only in the last cell and not at all....why?这是一个带有 15 个自定义 UITableViewCell 的 UITableView,我想在 UIImageView 上更改图像,这个 ImageView 是“lineDay”,它在所有单元格中都需要更改,但在所有单元格中都需要更改UIImageView 仅在最后一个单元格中,根本没有....为什么?

yes it will change the image in last cell because it has the reference of only last cell, if you want to do this then when you are creating cell in cellForRowAtIndexPath check for a BOOL and set image according that BOOL.是的,它会更改最后一个单元格中的图像,因为它只有最后一个单元格的引用,如果你想这样做,那么当你在 cellForRowAtIndexPath 中创建单元格时,检查 BOOL 并根据该 BOOL 设置图像。 Also in IBAction change that BOOL value and call reloadData on table view to reload it.同样在 IBAction 中更改该 BOOL 值并在表视图上调用 reloadData 以重新加载它。 As -作为 -

    - (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];
}

First, lineaRedToday is an UIImage , not an UIImageView .首先, lineaRedToday 是UIImage ,而不是UIImageView The first is an image, the second an object in the view hierarchy.第一个是图像,第二个是视图层次结构中的 object。

Then, in your code然后,在您的代码中

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

you use tblCell .你使用tblCell I cannot see what this is.我看不出这是什么。 You should assign something from what you were just loading from the nib.您应该从刚从笔尖加载的内容中分配一些内容。

I don't know what lineDay is exactly, but a single view (ie UIImageView ) can only be present in one cell, not in many.我不知道lineDay到底是什么,但是一个视图(即UIImageView )只能出现在一个单元格中,而不是多个单元格中。 If you change the image ( lineaRedToday ), you still have to set this new image in your views.如果您更改图像 ( lineaRedToday ),您仍然需要在视图中设置这个新图像。 There should be something like应该有类似的东西

cell.yourImageView.image = linaRedToday;

when configuring the cell.配置单元格时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM