iOS 表格视图单元格处理

发布于 2024-11-03 21:03:36 字数 1068 浏览 0 评论 0原文

我有这段代码,其中“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,而根本没有更改......为什么?

I have this code, where "lineaRedToday" is an 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必了 2024-11-10 21:03:36

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

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. Also in IBAction change that BOOL value and call reloadData on table view to reload it. 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];
}
生寂 2024-11-10 21:03:36

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

然后,在您的代码中

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

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

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

cell.yourImageView.image = linaRedToday;

配置单元时

First, lineaRedToday is an UIImage, not an UIImageView. The first is an image, the second an object in the view hierarchy.

Then, in your code

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

you use 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 (i.e. UIImageView) can only be present in one cell, not in many. If you change the image (lineaRedToday), you still have to set this new image in your views. There should be something like

cell.yourImageView.image = linaRedToday;

when configuring the cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文