1

你好我是iOS的初学者我已经创建了自定义表格视图并且我有两个数组然后我想在表格视图中同时将两个数组数据一起设置为一个单元格内容我显示我想要的图像..

请参考:http: //i.stack.imgur.com/WIkaf.png

  NSMutableArray *SearchPatientCode;
  NSMutableArray *SearchPatientName;
  UITableView *table_SearchPatient;
  SearchPatientCode=[[NSMutableArray alloc]initWithObjects:@"PH230130420",@"PH230420321",@"Ph450362120", nil];

  SearchPatientName=[[NSMutableArray alloc]initWithObjects:@"Rahul Sharma",@"Amit   kumar",@"anil sharma", nil];

 table_SearchPatient=[[UITableView alloc]initWithFrame:CGRectMake(130,40,170,250)style:UITableViewStylePlain];
 table_SearchPatient.delegate=self;
 table_SearchPatient.dataSource=self;
 table_SearchPatient.layer.borderWidth = 2.0;
 table_SearchPatient.layer.borderColor = [UIColor grayColor].CGColor;
 [self.view addSubview:table_SearchPatient];

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
  {
  static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if(tableView==table_SearchPatient)
{

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

    }

    cell.textLabel.text =[NSString stringWithFormat:@"%@ /n %@",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:10.0f];
   // cell.detailTextLabel.text=[SearchPatientName objectAtIndex:indexPath.row];
}
return cell;
 }

我正在使用这个但这不是我想要的显示...解决这个问题!

4

4 回答 4

0

还要确保查看免费的Sensible TableView框架。给定数组,框架将自动显示表格视图中的所有值,甚至会在适用的情况下生成详细视图。为我节省了大量时间。

于 2013-07-10T18:49:37.173 回答
0

像下面这样添加新UILable...numberOfLines = 2

如果您想无限地使用您的内容数据,那么您可以使用 setnumberOfLines = 0和 setlineBreakMode = UILineBreakModeWordWrap

       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
          {
              static NSString *MyIdentifier = @"MyIdentifier";

              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

              if(tableView==table_SearchPatient)
              {
                 if (cell == nil)
                 {
                     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
                 }
                 UILabel *lbl = [[UILabel alloc]init];
                 lbl.text = [NSString stringWithFormat:@"%@ /n %@",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]];
                 lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:10.0f];
                 [lbl setFrame:CGRectMake(20, 2, 250, 35)];// set frame which you want
                 [lbl setBackgroundColor:[UIColor clearColor]];// set any color here
//                 lbl.lineBreakMode = UILineBreakModeWordWrap; // set it if you want to height of UILable with its content (Multiple line)
                 lbl.numberOfLines = 2;// Add this line // Set 0 if you want to multiple line.
                 [cell.contentView addSubview:lbl];
             }
             return cell;
          }
于 2013-07-10T05:34:35.780 回答
0

您可以使用详细信息cell.detailTextLabel.text

或者您可以创建一个自定义单元格并将标签添加到单元格,如下所示

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

}

cell.textLabel.text = @"";
label_name_one = [[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)]autorelease];
[label_name_one setText:[SearchPatientCode objectAtIndex:indexPath.row]];

label_name_two = [[[UILabel alloc]initWithFrame:CGRectMake(92, 30, 170, 80)]autorelease];
[label_name_two setText:[SearchPatientCode objectAtIndex:indexPath.row]];

根据您的要求设置 RectFrame 。

于 2013-07-10T05:55:52.560 回答
0

首先将您的单元格样式更改为UITableViewCellStyleValue2然后将此代码用于单元格中的多行文本:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"MyIdentifier";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                      reuseIdentifier:cellIdentifier];

        [[cell textLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:10.0f]];
    }

    [[cell detailTextLabel] setText:[NSString stringWithFormat:@"%@  %@",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]]];
    cell.detailTextLabel.numberOfLines = 2;
    cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
     return cell;
}

希望它可以帮助你。

于 2013-07-10T06:15:08.187 回答