【问题标题】:After a row is selected in a table view how do I access the rows not selected text label to chage its color在表格视图中选择一行后,如何访问未选择的行文本标签以更改其颜色
【发布时间】:2011-07-30 17:35:31
【问题描述】:

在显示表格视图并选择一行后,我想将所有未选择的行表格文本标签颜色更改为蓝色。当前在 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;
    }
}

【问题讨论】:

    标签: iphone objective-c xcode xcode4


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      因此,在tableView:cellForRowAtIndexPath:

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

      干杯,
      萨沙

      【讨论】:

      • 感谢您的快速响应。我仍然对 tableView:cellForRowAtIndexPath: 部分感到困惑。调用该方法始终指向 indexPath,它始终是所选单元格。我是否调用 tableView:cellForRowAtIndexPath: 使用我保存的上一行以及我该怎么做.. 希望这个问题有意义..
      • 无需致电tableView:cellForRowAtIndexPath:。它是通过调用上面的[tableView reloadData] 自动完成的。
      • 非常感谢 [tableView reloadData]... 现在可以完美运行了!!!!!!!!!!!!!!!!!!!!!
      • 您可能需要考虑单击该复选标记以将答案标记为已接受。谢谢。
      【解决方案3】:
      - (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;
              }
          }
      }
      

      希望对您有所帮助 快乐编码

      【讨论】:

        【解决方案4】:
        `- (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...`
        

        【讨论】:

          猜你喜欢
          • 2022-01-18
          • 2021-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-27
          • 2015-07-04
          • 2017-09-23
          • 1970-01-01
          相关资源
          最近更新 更多