这个问题可能会以不同的方式提出,但我的条件是另一种方式,所以在设置重复之前请检查这一点。从根视图我调用右视图,当我按下一行时,右视图有一些选项,自定义单元格标签值应该更改为我做过的其他值。但是当我调用根视图并再次调用右视图时,标签值变为原始值,那么如何在视图重新加载时为自定义单元格标签值保持相同的值?
最初我为标签分配值是
(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";
}
}
}
}
}
即使视图再次重新加载,如何保持相同的值,但另一个条件是如果我再次按单元格,它应该更改为以前的值并且正在使用自定义单元格
你走在一条很好的轨道上 - 你有自定义单元格,这很有帮助。
为自定义单元格创建另一个属性:
@property (nonatomic, retain) NSString* selectedText;
@property (nonatomic, retain) NSString* deselectedText;
@property (nonatomic, assign) BOOL selected;
当您在单元格中创建单元格时:初始化两个文本:
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;
}
}