如果已按下单元格中的图片,如何更改图片?

时间:2015-11-22 08:36:21

标签: ios

我有一个TableView。如果已按下单元格中的图片,该如何更改?

此代码不起作用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: [NSString stringWithFormat:@"Cell%d", indexPath.row] forIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *level =[[NSUserDefaults standardUserDefaults] stringForKey:@"level"];
int intForStrin =[level intValue];
NSString *level11 =[[NSUserDefaults standardUserDefaults] stringForKey:@"level11"];
int index =[level11 intValue];
UIImageView *imgView;
imgView =[[UIImageView alloc] initWithFrame:CGRectMake(24, 13, 30, 30)];
[cell.contentView addSubview:imgView];

if (indexPath.row == 0 && index == 0) {
    if (intForStrin <= 6) {
        imgView.image = [UIImage imageNamed:@"2.png"];
    }
    else if (intForStrin <= 11) {
        imgView.image = [UIImage imageNamed:@"3.png"];
    }
    else if (intForStrin <= 16) {
        imgView.image = [UIImage imageNamed:@"4.png"];
    }
    else if (intForStrin <= 20) {
        imgView.image = [UIImage imageNamed:@"5.png"];
    }
}
if (indexPath.row == 1 && index == 1) {
    if (intForStrin <= 6) {

        imgView.image = [UIImage imageNamed:@"2.png"];
    }
    else if (intForStrin <= 11) {
        imgView.image = [UIImage imageNamed:@"3.png"];
    }
    else if (intForStrin <= 16) {
        imgView.image = [UIImage imageNamed:@"4.png"];
    }
    else if (intForStrin <= 20) {
        imgView.image = [UIImage imageNamed:@"5.png"];
    }
}

----------------------------------------------- ----------------------------

1 个答案:

答案 0 :(得分:1)

在tableview api中使用didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath 
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageview.image = "yourimage";
}