【问题标题】:Objective c different uilabel in a row of a table cell目标c在表格单元格的一行中使用不同的uilabel
【发布时间】:2016-11-11 09:48:04
【问题描述】:

我正在尝试创建一个在每一行中都包含一些 uilabels 的表。 uilabel 的数量基于我的数组,如果我的数组有一个字符串,那么它只有一个标签,如果我的数组有 2 个字符串,那么该行将有两个标签。

第一行:

第二行:

这是我将字符串放入已经存在的 uilabels 中的代码。如果我有更多的字符串,如何让单元格自动生成更多的 uilabels?

- (void)updateCell:(NSString *)text1 label2:(NSString *)text2 label3:(NSString *)text3{
   self.testLabel1.text = text1;
   self.testLabel2.text = text2;
   self.testLabel3.text = text3;
}

- (void)createLabel{
   //create uilabels based on the size of array?
}

如果这不可行,还有什么替代方法?非常感谢任何建议。

【问题讨论】:

    标签: ios objective-c uitableview uilabel


    【解决方案1】:

    这一切都应该在 cellForRowAtIndexPath 中完成,您应该基本上使用您的数组作为该行索引,然后以编程方式创建 uilabels,这是一些代码,它不会工作,但逻辑应该没问题

     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray *stringLabels = [self.labels objectAtIndex:indexPath.row]; // im not sure how you are getting the array
    
        for (NSString *str in stringLabels) {
            UILabel *newLabel = [[UILabel alloc]init];
            newlabel.SetFrame = CGrectMake(Set to your desired layout);
            newlabel.text = str;
            // uilabel formatting
    
            [cell addSubview:newLabel]
        }
    
    }
    

    这种方法非常灵活,因为它允许您在每个单元格中拥有任意数量的标签。

    【讨论】:

      【解决方案2】:

      试试这个

      您需要根据您的条件创建不同的单元格并配置 CellForAtIndexPath 如下:

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
          NSString *strFirstIndentifier = @"FirstIndentifier"; // set this idenfier to UITableViewCell in StroryBoard for all
          NSString *strSecondIndentifier = @"SecondIndentifier";
          NSString *strThirdIndentifier = @"ThirdIndentifier";
          UITableViewCell *adjustcell ;
          if (arrUnits.count == 1) {
              adjustcell = [tableView dequeueReusableCellWithIdentifier:strFirstIndentifier];
              //configure your lables here
          }
          else if (arrUnits.count == 2) {
              adjustcell = [tableView dequeueReusableCellWithIdentifier:strSecondIndentifier];
              //configure your lables here
          }
          else if (arrUnits.count == 3) {
              adjustcell = [tableView dequeueReusableCellWithIdentifier:strThirdIndentifier];
              //configure your lables here
          }
      
          return adjustcell;
      }
      

      【讨论】:

        【解决方案3】:

        我建议将UICollectionViewUICollectionView 的数据源一起使用是一个数组,该数组将保存该特定行中的字符串数。 UICollectionView 的每个单元格将包含一个标签。我个人觉得这种方式更容易管理。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多