xcode 在表视图中选择一行后,如何访问未选择的行文本标签以更改其颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6884824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:36:49  来源:igfitidea点击:

After a row is selected in a table view how do I access the rows not selected text label to chage its color

iphoneobjective-cxcodexcode4

提问by Frank Boscarello

After a table View is displayed and a row is selected I want to change the color of all the non-selected rows table text label color to blue.. Current within the case statement I can change the current row selected text label but with that case I don't know how to access the other rows text label.

显示表格视图并选择一行后,我想将所有非选定行表格文本标签颜色的颜色更改为蓝色.. 当前在 case 语句中我可以更改当前行选定的文本标签,但在这种情况下我不知道如何访问其他行文本标签。

- (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;
    }
}

回答by Jonah

Managing cell selection state from your table view's delegate is going to be unnecessarily difficult. You could store the index path of the selected cell, call the table view's cellForRowAtIndexPath:method to get the formerly selected cell, and reset the label's color. However you don't know that the formerly selected cell is even still visible or loaded.

从表格视图的委托中管理单元格选择状态将变得非常困难。您可以存储所选单元格的索引路径,调用 table view 的cellForRowAtIndexPath:方法获取以前选择的单元格,并重置标签的颜色。但是,您不知道以前选择的单元格仍然可见或已加载。

Better to use a UITableViewCell subclass which implements setSelected:to update its own view. That way your table view delegate only needs to set the cell's selected property and the cell is responsible for updating its own view appropriately. That will also allow you to implement perpareForReuseto reset your cells' views so that you don't need to worry about reusing a formerly selected cell and unexpectedly displaying a cell in a selected state.

最好使用实现setSelected:更新自己视图的 UITableViewCell 子类。这样你的表格视图委托只需要设置单元格的 selected 属性,单元格负责相应地更新它自己的视图。这也将允许您实现perpareForReuse重置单元格的视图,这样您就不必担心重用以前选择的单元格并意外地显示处于选定状态的单元格。

回答by Mundi

Keep a variable, e.g. an NSIntegercalled selectedCell, that tracks the selected cell. Change it to the selected cell and then reload the visible cells. In tableView:cellForRowAtIndexPath:you check the variable and style the label accordingly.

保留一个跟踪所选单元格的变量,例如一个NSInteger被调用的变量selectedCell。将其更改为选定的单元格,然后重新加载可见单元格。在tableView:cellForRowAtIndexPath:您检查变量并相应地设置标签样式时。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedCell = indexPath.row;
    [tableView reloadData];
}

Thus, in tableView:cellForRowAtIndexPath:

因此,在 tableView:cellForRowAtIndexPath:

// format the cell...
if (indexPath.row==self.selectedCell) {
    // style your cell for selected state
}
else {
    // style your cell for non-selected state
}

Cheers,
Sascha

干杯,
萨沙

回答by GuruMurthy

- (void)tableView:(UITableView *)tableView 
     didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  [self setSelectedImageAndTextColorForTableCell:indexPath :tableView];
}

//THIS METHOD IS USED TO RESET THE SELECTED COLORS FOR UIIMAGEVIEW AND UILABEL------
- (void)setSelectedImageAndTextColorForTableCell:(NSIndexPath *)aIndexPath 
               :(UITableView *)aTableView {
    if (!self.LastIndexPath_) {
        if (aTableView == mTableView_) {
            [self setLastIndexPath_:aIndexPath];
        }
    }
    if (self.mLastIndexPath_.row != aIndexPath.row) {
        //If Old Cell.

      UITableViewCell *lOldCell = [aTableView cellForRowAtIndexPath:self.mLastIndexPath_];
      UILabel *lLabel_=(UILabel*)[lOldCell.contentView viewWithTag:1];
      [lLabel_ setTextColor:RGB_A(35, 30, 32, 1)];

      //If User touches on new cell.

      UITableViewCell *lNewCell = [aTableView cellForRowAtIndexPath:aIndexPath];
      UILabel *lLabel_ = (UILabel*)[lNewCell.contentView viewWithTag:1];
      [lLabel_ setTextColor:[UIColor whiteColor]];
      self.mLastIndexPath_ = aIndexPath;


    }else {
        UITableViewCell *lNewCell = [aTableView cellForRowAtIndexPath:aIndexPath];
        if (aTableView == mTableView_) {
            UITableViewCell *lNewCell = [aTableView cellForRowAtIndexPath:aIndexPath];
            UILabel *lLabel_ = (UILabel*)[lNewCell.contentView viewWithTag:1];
            [lLabel_ setTextColor:[UIColor whiteColor]];
            self.mLastIndexPath_ = aIndexPath;
        }
    }
}

Hope this will help you Happy coding

希望这会帮助你快乐编码

回答by GuruMurthy

`- (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...`