objective-c - 维护从 xib 加载的自定义单元格内的按钮状态

标签 objective-c ios uitableview

我有一个表格 View ,其中包含从 xib 加载的自定义单元格 该单元格有一个按钮,按下时会更改其图像 我的问题是,当我在 View 之外上下滚动表格时,按钮重新加载它的默认图像

这是我的代码

表格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifer=@"LabelCell";
    TestTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifer];

   if (cell==nil) 
    {
       cell = [[TestTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"TestTableCell" owner:nil options:nil];

        for (UIView *view in views) 
        {
            if([view isKindOfClass:[TestTableCell class]])
            {
                cell = (TestTableCell*)view;
            }
        }

    cell.selectionStyle=UITableViewCellSeparatorStyleNone;
    UIImage *cellbackground=[[UIImage imageNamed:@"cell-background.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    cell.backgroundView=[[UIImageView alloc]initWithImage:cellbackground];

   }
    return  cell;
}

最佳答案

您需要在数据模型中存储图像是否已被按下。当您滚动时,UITableView 会将您的单元格重用于其他行,并且单元格内的所有内容都将被忘记。

关于objective-c - 维护从 xib 加载的自定义单元格内的按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12411446/

相关文章:

ios - didSelectRowAt 不适用于我的 UITableViewController

ios - 地址簿 iOS : Contact name from string

iOS 通过手指滑动绘制人字形图案

ios - Foodspotting 中的界面设计

iphone - ios : crashing after including coredata to old project

ios - 在swift中添加任意两个数字元组

ios - 如何在自定义单元格中添加按钮?

objective-c - 不鼓励使用 beginAnimations

ios - 一个 View Controller 中的两个表

ios - calendarsForEntityType返回空数组