Iphone 在表视图中选择行后,如何访问未选择的行文本标签以更改其颜色
显示表格视图并选择行后,我想将所有未选择行的颜色表格文本标签颜色更改为蓝色。。当前在case语句中,我可以更改当前行的选定文本标签,但在这种情况下,我不知道如何访问其他行的文本标签Iphone 在表视图中选择行后,如何访问未选择的行文本标签以更改其颜色,iphone,objective-c,xcode,xcode4,Iphone,Objective C,Xcode,Xcode4,显示表格视图并选择行后,我想将所有未选择行的颜色表格文本标签颜色更改为蓝色。。当前在case语句中,我可以更改当前行的选定文本标签,但在这种情况下,我不知道如何访问其他行的文本标签 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.row) { case 0: UITableVi
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor yellowColor];
//******* NEED TO CHANGE ALL OTHER ROWS TEXT LABEL TO BLUE
break;
case 1:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor yellowColor];
//******* NEED TO CHANGE ALL OTHER ROWS TEXT LABEL TO BLUE
break;
default:
break;
}
}
从表视图的委托管理单元格选择状态将是不必要的困难。您可以存储所选单元格的索引路径,调用表视图的
cellforrowatinexpath:
方法来获取以前选择的单元格,并重置标签的颜色。但是,您不知道以前选择的单元格仍然可见或已加载
最好使用实现
setSelected:
的UITableViewCell子类来更新自己的视图。这样,您的表视图委托只需要设置单元格的选定属性,并且单元格负责适当地更新自己的视图。这还将允许您实现perpareForReuse
重置单元格的视图,这样您就不必担心重用以前选定的单元格并意外显示处于选定状态的单元格。保留一个变量,例如名为selectedCell
的NSInteger
,用于跟踪选定单元格。将其更改为选定单元格,然后重新加载可见单元格。
在tableView:cellforrowatinexpath:
中,检查变量并相应设置标签样式
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedCell = indexPath.row;
[tableView reloadData];
}
因此,在tableView:cellforrowatinexpath:
// format the cell...
if (indexPath.row==self.selectedCell) {
// style your cell for selected state
}
else {
// style your cell for non-selected state
}
干杯,萨沙 希望这对你有帮助 快乐编码
`-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
`- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (nil==cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0)] autorelease];
cell.contentView.backgroundColor=CLEAR_COLOR;
//Configure cell format for the tableView,
//Same format for all the three sections.
int tagCount = 1;
CGFloat lXCoord = 5,lYCoord = 7,lWidth = 100/*,lHeight = 18*/;
// for (tagCount = 1; tagCount <= 1; tagCount++) {
UILabel *lUILabel = [[UILabel alloc]init];
lUILabel.frame = CGRectMake(lXCoord+10, 0, lWidth, 34);
lUILabel.backgroundColor = [UIColor clearColor];
lUILabel.textColor = RGB_A(35, 30, 32, 1);
lUILabel.highlightedTextColor=WHITE_COLOR;
lUILabel.font = FONT_OPENSANS_REGULAR(14);
[cell.contentView addSubview:lUILabel];
[lUILabel setTag:tagCount];
RELEASE_NIL(lUILabel);
// }
UIImageView *lUIImageView = [[UIImageView alloc]init];
lUIImageView.frame = CGRectMake(lXCoord+100, lYCoord, 45, 29);
[lImageView_ setHighlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"iPad_BuildInspectionDetails_%@_Selected.png",[[mDamageTableArray_ objectAtIndex:0] objectAtIndex:indexPath.row]]]];
[lImageView_ setImage:[UIImage imageNamed:[NSString stringWithFormat:@"iPad_BuildInspectionDetails_%@_Normal.png",[[mDamageTableArray_ objectAtIndex:0] objectAtIndex:indexPath.row]]]];
cell.accessoryView=lUIImageView;
[lUIImageView setTag:tagCount + 1];
RELEASE_NIL(lUIImageView);
UIView *lSelectedView_ = [[[UIView alloc] init] autorelease];
lSelectedView_.backgroundColor =SELECTED_CELL_COLOR_GM;
cell.selectedBackgroundView = lSelectedView_;
}
//cell.accessoryType=UITableViewCellAccessoryNone;
return cell;
}
- Note :-
Use setHighlightedImage property for UIImageView `enter code here`& highlightedTextColor for UILabel.
you can see highlighted image and highlighted text color if you select a row in UITableView
This will help you.
Happy Coding...`
UITableViewCell*单元格=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@“CellIdentifier”];
if(nil==单元格)
{
cell=[[UITableViewCell alloc]initWithFrame:CGRectMake(0,0,0,0)]autorelease];
cell.contentView.backgroundColor=清晰的颜色;
//为tableView配置单元格格式,
//所有三个部分的格式相同。
int tagCount=1;
CGFloat lXCoord=5,lYCoord=7,lWidth=100/*,lHeight=18*/;
//为了(tagCount=1;tagCount感谢您的快速响应..我仍然对tableView:CellForRowatineXpath:part.有点困惑..调用该方法总是指向indexPath,而indexPath始终是选定的单元格。我是否调用tableView:CellForRowatineXpath:使用我保存的前一行以及如何做..希望这个问题有意义。。不需要调用<代码> TabelVIEW:CyfFurWaldRexXPosi:<代码>。它是通过调用<代码> [TabeVIEW ReaDebug ] >代码>自动完成的。非常感谢[ TabLeVIEW ReaDeDATABAS]……使它完美地工作了!!!!!!!您可能想考虑点击那个复选标记来将答案标记为已接受。谢谢。
`- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (nil==cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0)] autorelease];
cell.contentView.backgroundColor=CLEAR_COLOR;
//Configure cell format for the tableView,
//Same format for all the three sections.
int tagCount = 1;
CGFloat lXCoord = 5,lYCoord = 7,lWidth = 100/*,lHeight = 18*/;
// for (tagCount = 1; tagCount <= 1; tagCount++) {
UILabel *lUILabel = [[UILabel alloc]init];
lUILabel.frame = CGRectMake(lXCoord+10, 0, lWidth, 34);
lUILabel.backgroundColor = [UIColor clearColor];
lUILabel.textColor = RGB_A(35, 30, 32, 1);
lUILabel.highlightedTextColor=WHITE_COLOR;
lUILabel.font = FONT_OPENSANS_REGULAR(14);
[cell.contentView addSubview:lUILabel];
[lUILabel setTag:tagCount];
RELEASE_NIL(lUILabel);
// }
UIImageView *lUIImageView = [[UIImageView alloc]init];
lUIImageView.frame = CGRectMake(lXCoord+100, lYCoord, 45, 29);
[lImageView_ setHighlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"iPad_BuildInspectionDetails_%@_Selected.png",[[mDamageTableArray_ objectAtIndex:0] objectAtIndex:indexPath.row]]]];
[lImageView_ setImage:[UIImage imageNamed:[NSString stringWithFormat:@"iPad_BuildInspectionDetails_%@_Normal.png",[[mDamageTableArray_ objectAtIndex:0] objectAtIndex:indexPath.row]]]];
cell.accessoryView=lUIImageView;
[lUIImageView setTag:tagCount + 1];
RELEASE_NIL(lUIImageView);
UIView *lSelectedView_ = [[[UIView alloc] init] autorelease];
lSelectedView_.backgroundColor =SELECTED_CELL_COLOR_GM;
cell.selectedBackgroundView = lSelectedView_;
}
//cell.accessoryType=UITableViewCellAccessoryNone;
return cell;
}
- Note :-
Use setHighlightedImage property for UIImageView `enter code here`& highlightedTextColor for UILabel.
you can see highlighted image and highlighted text color if you select a row in UITableView
This will help you.
Happy Coding...`