0

我在侧边栏中有表格视图,类似于 facebook 应用程序。在我的侧边栏表格视图中,我在每一行都有图像视图。我希望在单击单元格时更改图像视图,并在选择另一个单元格时保留它。这是我的代码

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
[contentView addSubview:cellImg];

[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

 #pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(@"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);

[UIView commitAnimations];
if (self.sidebarDelegate) {
    NSObject *object = [NSString stringWithFormat:@"ViewController%d", indexPath.row];
    [self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
    [delegate.firstLensePageObj timerFunction:indexPath.row];
}

}
4

3 回答 3

1

didSelectRowAtIndexPath在你的委托方法中试试这个: -

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"yourImage"];
于 2013-10-31T05:45:00.567 回答
0

在 cellForRowAtIndexPath 中试试这个。

  UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Your Image"]];

  cell.selectedBackgroundView = selectedImageView;
于 2013-10-31T05:56:12.383 回答
0

需要在didSelectRowAtIndexPath:方法中捕获选定的 indexpath,并且需要在该方法中重新加载 tableview。在cellForRowAtIndexPath中需要用当前索引检查选中的索引。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

    UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
if (delegate.sideBarSelectedIndex == indexpath.row){
//Selected cell
    [cellImg setImage:[UIImage imageNamed:[selectedMenuArr objectAtIndex:indexPath.row]]];
}else {
//Unselected cell
    [cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
}
    [contentView addSubview:cellImg];

    [contentView setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:contentView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    }

     #pragma mark - Table view delegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    delegate.sideBarSelectedIndex = indexPath.row;
    NSLog(@"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);

    [UIView commitAnimations];
    if (self.sidebarDelegate) {
        NSObject *object = [NSString stringWithFormat:@"ViewController%d", indexPath.row];
        [self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
        [delegate.firstLensePageObj timerFunction:indexPath.row];
    }

    }
于 2016-12-10T06:52:32.500 回答