0

这个问题可能会以不同的方式提出,但我的情况是另一种方式,所以在设置重复检查之前。从根视图我调用右视图,当我按下一行时,右视图有一些选项,自定义单元格标签值应该更改为我已经完成的其他值。但是,当我再次调用根视图并再次调用右视图时,标签值会变为原始值,那么当视图重新加载时如何为自定义单元格标签值保持相同的值?

最初为标签赋值的是

(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0)
        {
            cell.rightpanelLabel1.text=@"Switch to Photo View";
        } 
}

然后当单元格按下时,我像这样更改了标签值

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell=[[ViewProfileRightPanelCell alloc]init];
     static NSString *cellidentifier=@"ViewProfileRightPanelCell";
    cell=(ViewProfileRightPanelCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
    for (cell in [ViewProfileRightPanelTableView subviews])
    {
        if ([cell isKindOfClass:[ViewProfileRightPanelCell class]])
        {
            if (cell.tag == indexPath.row)
            {

                if ([cell.rightpanelLabel1.text isEqualToString:@"Switch to Photo View"] )
                {
                    cell.rightpanelLabel1.text=@"Switch to List View";
                }

            }
        }
    }
}

即使再次重新加载视图,如何保持相同的值但另一个条件是如果我再次按下单元格,它应该更改为以前的值并且正在使用自定义单元格

4

1 回答 1

0

你走在一个很好的轨道上——你有自定义单元格,这很有帮助。

为您的自定义单元格创建另一个属性:

@property (nonatomic, retain) NSString* selectedText;
@property (nonatomic, retain) NSString* deselectedText;
@property (nonatomic, assign) BOOL selected;

当您在 cellForRowAtIndexPath 中创建单元格时:初始化两个文本:

if(indexPath.row==0)
{
    cell.selectedText= @"Switch to Photo View";
    cell.deselectedText = @"Switch to List View";
    cell.selected = NO;
} 

和:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell=[[ViewProfileRightPanelCell alloc]init];
     static NSString *cellidentifier=@"ViewProfileRightPanelCell";
    cell=(ViewProfileRightPanelCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
    for (cell in [ViewProfileRightPanelTableView subviews])
    {
        if ([cell isKindOfClass:[ViewProfileRightPanelCell class]])
        {
            if (cell.tag == indexPath.row)
            {
                cell.selected = !cell.selected;
            }
            else
            {
                cell.selected = NO;
            }
        }
    }
}

并在您的自定义单元格上为“选定”属性制作自定义设置器

- (void) setSelected:(BOOL) selected
{
     _selected = selected;
     if(selected)
    {
         self.rightpanelLabel1.text = self.selectedText;
    }
    else
    {
         self.rightpanelLabel1.text = self.deselectedText;
    }
}
于 2013-09-17T08:08:31.270 回答