首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改自定义表视图单元格的图像

更改自定义表视图单元格的图像
EN

Stack Overflow用户
提问于 2017-01-10 04:21:38
回答 3查看 125关注 0票数 0

My :,我创建了一个名为:BestQuestionListCell的自定义单元格,并在BestQuestionListViewController中创建了一个tableview。在BestQuestionListCell中,我在左边添加了一个按钮。我setImage在button上。现在,当我点击单元格时,button的图像确实发生了变化。现在,当视图出现时,我希望将其中一个单元格设置为选中。

我尝试设置按钮的图像,也尝试将单元格设置为选定的。他们中的任何一个都没用。

UISreenShot : BestQuestionListViewController的截图

这里是tableViewcell.的代码

代码语言:javascript
复制
@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);
}

,这里是tableview.

代码语言:javascript
复制
-(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;
}

我在里什么都没做

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

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

代码语言:javascript
复制
[mainCell setButtonImage:[UIImage imageNamed:@"Fstar"]];
[mainCell.starButton setImage:[UIImage imageNamed:@"Fstar"] forState:UIControlStateNormal];
[mainCell setSelected:YES animated:YES];
EN

回答 3

Stack Overflow用户

发布于 2017-01-10 04:32:33

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

代码语言:javascript
复制
#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

输出:

项目运行时最初的 :

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

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

票数 1
EN

Stack Overflow用户

发布于 2017-01-10 04:29:09

代码语言:javascript
复制
  //you could write only
 [mainCell setSelected:YES animated:YES];
票数 0
EN

Stack Overflow用户

发布于 2017-01-13 09:26:17

它适用于me.Please,试一试。

代码语言:javascript
复制
First set the IBOutLet in the tableViewCell.

然后在viewController:在cellForRowAtIndexPath方法中,您可以这样编写:

代码语言:javascript
复制
UIImage *imageSelected = [UIImage imageNamed:@"your selected img name"];
UIImage *imageNotSelected = [UIImage imageNamed:@"your not selected img name"];
[cell.yourBtnName setImage:imageNotSelected forState:UIControlStateNormal];
[cell.yourBtnName setImage:imageSelected forState:UIControlStateSelected];

[cell.yourBtnName addTarget:self action:@selector(yourBtnClickMethod:) forControlEvents:UIControlEventTouchUpInside];

然后在行动方法中:

代码语言:javascript
复制
-(void)yourBtnClickMethod:(UIButton *)btn{
    btn.selected = !btn.selected;
}

希望它对you.Thanks有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41561092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档