【问题标题】:How to display all text label values in custom label in tabel view?如何在表格视图的自定义标签中显示所有文本标签值?
【发布时间】:2013-09-14 05:49:46
【问题描述】:

我是 iphone 开发新手。我正在使用一个 tableView,它加载一个 UITableViewCell,里面有一个 UIlabel。在 cellForRowAtIndexPath 中,我想将所有单元格 textLabel 值存储在自定义 UILabel 中。只有一个表格视图单元格行。我们的 UILabel 框架与我在代码中提到的表格单元框架相同。从可变数组中,我得到不同的 indexPath 行单元格文本标签值。

这是我的代码..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Cuisine *cuisine = [[Cuisine alloc] init];// Cuisine is my object entity class..

    //Custom UILabel

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 3, 310, 30)];
    label.textColor = [UIColor colorWithRed:57/255 green:57/255 blue:57/255 alpha:1.0];
    label.font = [UIFont fontWithName:@"HelveticaNeue" size:14];

    NSLog(@"array is = %@", array);
    label.text = [[array objectAtIndex:indexPath.row] cuisineName];// here i am getting  
    first cuisine name string value for 0 indexPath row.

    [cell addSubview:label];

   }
 return finalCell;
}

使用此代码,我得到了 indexPath 第 0 行的第一个美食名称字符串值。那么如何在 UILabel 文本中显示所有数组的所有字符串值?

输出应该是这样的..

name1,name2,name3,name4,nam.....

提前谢谢..

【问题讨论】:

  • 如果你只有一行,为什么要使用表格视图?
  • @rdelmarso 而不是表格视图,我可以只使用 UILabel。
  • 但我还需要在单元格上设置委托。
  • 当然,为什么不呢。您可以使用 stringWithFormat 或 stringByAppendingString 来构建一个包含数组中所有字符串的字符串。
  • 为什么需要设置委托?

标签: iphone ios uitableview


【解决方案1】:

只需像这样创建一个包含所有数组值的字符串,只需创建一个全局字符串,然后在填充此数组的任何位置使用该全局字符串执行此操作并在任何地方访问

NSString *str = @"";

for(int i=0;i<array.count;i++)
{
    NSString *cuisineName = [[array objectAtIndex:i] cuisineName]; 
    str = [str stringByAppendingFormat:@",%@",cuisineName];

}

现在在您的标签 Simple 中显示此字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多