Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITableViewCell上保存按钮选择?_Ios_Uitableview - Fatal编程技术网

Ios 如何在UITableViewCell上保存按钮选择?

Ios 如何在UITableViewCell上保存按钮选择?,ios,uitableview,Ios,Uitableview,我的应用程序有一个最喜欢的按钮,点击它就会变成红心,如果我再次点击,它就会变成灰心。它工作正常。但问题是重用标识符,在滚动之后,按钮刚刚恢复到原始状态,因为它的恢复单元在这里 如何保存对按钮的选择,使其保持选中状态 tableViewCell类.h文件的代码: viewcontroller.m的代码 您的单元格需要相应的数据源。例如,为此,您可以再创建一个类,并将其用作数据源的项。看一看: @interface DataSourceItem : NSObject @property (nonat

我的应用程序有一个最喜欢的按钮,点击它就会变成红心,如果我再次点击,它就会变成灰心。它工作正常。但问题是重用标识符,在滚动之后,按钮刚刚恢复到原始状态,因为它的恢复单元在这里

如何保存对按钮的选择,使其保持选中状态

tableViewCell类.h文件的代码:

viewcontroller.m的代码


您的单元格需要相应的数据源。例如,为此,您可以再创建一个类,并将其用作数据源的项。看一看:

@interface DataSourceItem : NSObject
@property (nonatomic, assign) BOOL isFavorite;
@end

@implementation DataSourceItem
@end
然后,在view controller的代码中,您需要填充数据源项数组,并根据此数组管理表视图:

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString *ci;
}
@property (strong, nonatomic) IBOutlet UITableView *tv;
@property (strong, nonatomic) NSArray *dataSourceArray;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
        _tv.delegate=self;
    _tv.dataSource=self;

    NSMutableArray *temp = [NSMutableArray new];
    // actually how many rows you table view need to have
    for (NSUInteger i = 0; i < 30; i++)
    {
        [temp addObject:[DataSourceItem new]];
    }
    self.dataSourceArray = [temp copy];**
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     // change dataSource item state
     DataSourceItem *item = [self.dataSourceArray objectAtIndex:indexPath.row];
    item.isFavorite = !item.isFavorite;
    // change cell state 
     favBTNTableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.faVbtn.selected = item.isFavorite;
}

-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.dataSource count];
}
-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    favBTNTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ci];

    if(!cell)

    {
        cell=[[favBTNTableViewCell alloc]init];
        cell.faVbtn.tag=indexPath.row;

    }

    // decision based on data source item state
    cell.favBtn.selected = self.dataSource[indexPath.row];
    return cell;


}

您可以这样修复它:

在自定义单元格中实现-voidlayoutSubviews方法。 在-voidLayoutSubview中,选中按钮状态,然后设置 右图: ->


如果您使用的是自定义单元格 在did load中为您的tableview注册类/nib

[tblName registerNib:[UINib nibWithNibName:@"customCell" bundle:nil] forCellReuseIdentifier:@"customCell"];
从此在数据源方法中

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    customCell *cell =(customCell*) [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
    // your code here
    return cell;
}
不要忘记将单元格标识符指定为customCell in cell属性 快乐编码

你也可以由我检查类似的ans

存储indexpath,根据这个填充单元格,我应该在哪里存储indexpath。若我在tableViewCell类中获取数组,那个么我并没有限定初始化它的任何位置@EICAPTANmake one mutableArray..全局初始化..并单击按钮添加或删除indexpath…这就是您应该管理indexpath数组的原因。所以,若用户按Favorite保存该indexpath,并在initWithStyle中检查该indexpath是否已在数组中,那个么显示redImage,否则显示greyImage。另外,当用户点击收藏夹时,检查indexPath是否已经在数组中,然后删除该indexPath
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString *ci;
}
@property (strong, nonatomic) IBOutlet UITableView *tv;
@property (strong, nonatomic) NSArray *dataSourceArray;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
        _tv.delegate=self;
    _tv.dataSource=self;

    NSMutableArray *temp = [NSMutableArray new];
    // actually how many rows you table view need to have
    for (NSUInteger i = 0; i < 30; i++)
    {
        [temp addObject:[DataSourceItem new]];
    }
    self.dataSourceArray = [temp copy];**
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     // change dataSource item state
     DataSourceItem *item = [self.dataSourceArray objectAtIndex:indexPath.row];
    item.isFavorite = !item.isFavorite;
    // change cell state 
     favBTNTableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.faVbtn.selected = item.isFavorite;
}

-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.dataSource count];
}
-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    favBTNTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ci];

    if(!cell)

    {
        cell=[[favBTNTableViewCell alloc]init];
        cell.faVbtn.tag=indexPath.row;

    }

    // decision based on data source item state
    cell.favBtn.selected = self.dataSource[indexPath.row];
    return cell;


}
- (void)layoutSubviews {
    [super layoutSubviews];

    UIImage *buttonImage = [faVbtn isSelected] ? [UIImage imageNamed:@"sel.jpg"] : [UIImage imageNamed:@"unsel"];
    [faVbtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
}
[tblName registerNib:[UINib nibWithNibName:@"customCell" bundle:nil] forCellReuseIdentifier:@"customCell"];
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    customCell *cell =(customCell*) [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
    // your code here
    return cell;
}