gpt4 book ai didi

ios - 更改自定义表格 View 单元格的图像

转载 作者:行者123 更新时间:2023-11-29 00:31:51 25 4
gpt4 key购买 nike

我的情况: 我制作了一个名为:BestQuestionListCell 的自定义单元格,并在 BestQuestionListViewController 中创建了一个tableview。在 BestQuestionListCell 中,我在左侧添加了一个按钮。我在 按钮 上设置图像。现在,当我点击单元格时,按钮 的图像确实发生了变化。现在我想在 View 显示时将这些单元格之一设置为选中状态。

我尝试设置按钮的图像,我还尝试将单元格设置为选中状态。他们都没有工作。

UISreenShot : The screenshot of BestQuestionListViewController

这里是tableViewcell的代码。

@implementation BestQuestionListCell
@synthesize questionLabel;
@synthesize authorLabel;
@synthesize ownerButton;
@synthesize starButton;

- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
NSLog(@"I am selected! %@",selected?@"yes":@"no");
// [super setSelected:selected animated:animated];
if (selected) {
[self.starButton setImage:[UIImage imageNamed:@"Fstar"] forState:UIControlStateNormal];
}else{
[self.starButton setImage:[UIImage imageNamed:@"Star"] forState:UIControlStateNormal];
}
}

- (void)setButtonImage:(UIImage *)buttonImage{
_buttonImage = buttonImage;
[self.starButton setImage:[UIImage imageNamed:@"Fstar"] forState:UIControlStateNormal];
[self setNeedsLayout];
NSLog(@"nothing happened? %@",buttonImage);
}

这是表格 View 。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifer = @"BestQuestionListCell";
BestQuestionListCell *mainCell = (BestQuestionListCell *)[self.bestQuestionListTableView dequeueReusableCellWithIdentifier:cellIdentifer];

if(mainCell == nil) {
mainCell = [[[NSBundle mainBundle] loadNibNamed:cellIdentifer owner:self options:nil] firstObject];
}

if ( [DataSession sharedSession].subQuestionArray.count > 0) {
mainCell.questionLabel.text = [[DataSession sharedSession].subQuestionArray[indexPath.row] question_content];
mainCell.questionLabel.textColor = [UIColor colorWithRed:74/255.0f green:74/255.0f blue:74/255.0f alpha:1.0f];
mainCell.questionLabel.backgroundColor = [UIColor whiteColor];
mainCell.selectionStyle = UITableViewCellSelectionStyleNone;

}
return mainCell;
}

我什么都没做

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

我尝试了这三种方法来解决这个问题,但没有奏效。

[mainCell setButtonImage:[UIImage imageNamed:@"Fstar"]];
[mainCell.starButton setImage:[UIImage imageNamed:@"Fstar"] forState:UIControlStateNormal];
[mainCell setSelected:YES animated:YES];

最佳答案

根据您在下面编辑的问题是代码工作。我已经完成了代码注释,以便您可以正确理解代码,如果您仍然有问题可以询问。

#import "BestQuestionListVC.h"
#import "BestQuestionListCell.h"

@interface BestQuestionListVC () <UITableViewDelegate, UITableViewDataSource>{
NSArray *arrQuesList;
NSArray *arrAuthorList;
NSMutableArray *arrMutBtnSelected; // arrMut of bools, containing true/false of button as selected or not.

}

@property (strong, nonatomic) IBOutlet UITableView *tblBestQuestion;


@end

@implementation BestQuestionListVC

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

// creating temp array of data
arrQuesList = @[@"ques #1", @"ques #2", @"ques #3", @"ques #4", @"ques #5", @"ques #6", @"ques #7", @"ques #8" ];
arrAuthorList = @[@"Author #1", @"Author #2", @"Author #3", @"Author #4", @"Author #5", @"Author #6", @"Author #7", @"Author #8" ];

arrMutBtnSelected = [NSMutableArray new]; // alloc Mutable array

// array of bools, initially all false as not any button is selected
for (int i=0; i < arrQuesList.count; i++) {
[arrMutBtnSelected addObject:[NSNumber numberWithBool:false]];
}

// for initially first button is selected.
[arrMutBtnSelected replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:true]];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)btnStarClicked:(UIButton *)sender {
// If one button is selected at one time
// start
/*
[arrMutBtnSelected removeAllObjects];
for (int i=0; i < arrQuesList.count; i++) {
[arrMutBtnSelected addObject:[NSNumber numberWithBool:false]];
}
[arrMutBtnSelected replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:true]];
*/
// end

// if user can selects multiple buttons
// start
/*
if (sender.selected) {
[arrMutBtnSelected replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:false]];
}
else{
[arrMutBtnSelected replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:true]];
}
*/
// end

[self.tblBestQuestion reloadData];
}


#pragma mark - TableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arrQuesList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BestQuestionListCell *quesCell = [tableView dequeueReusableCellWithIdentifier:@"BestQuestionListCell"];

quesCell.lblQuestion.text = arrQuesList[indexPath.row];
quesCell.lblAuthor.text = arrAuthorList[indexPath.row];
quesCell.btnStar.tag = indexPath.row ;

if (arrMutBtnSelected[indexPath.row] == [NSNumber numberWithBool:true]) {
quesCell.btnStar.selected = true;
}
else{
quesCell.btnStar.selected = false;
}

return quesCell;
}

@end

输出:

最初在项目运行时:

enter image description here

如果用户一次选择一个按钮,则输出:

enter image description here

如果用户选择任何多个按钮的输出

enter image description here

关于ios - 更改自定义表格 View 单元格的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com