2011-03-10 26 views
0

我对Xcode和Objective C非常陌生,但我似乎无法在我想要创建的简单表上找到教程。我想有一个只有三行但两列的表(分组样式)。列A将有一个标签(如'Name:'),而列B将有实际的数据(“Jason”)。有两个“列”的表视图?

我似乎无法找到如何做到这一点。除非我完全查找错误的东西?希望有人能够帮助我如何做到这一点,或指引我走向正确的方向。

谢谢!

+0

因为您是iOS新手我会推荐观看来自斯坦福大学Itunes U的CS193P课程视频http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=384233225 – 2011-03-10 14:57:38

回答

7

您不想使用具有2列的表格。它们不在iOS中使用。
您应该使用UITableViewCellUITableViewCellStyleValue2样式。并且您想将@"Name"设置为textLabel.text@"Jason",并将其设置为detailTextLabel.text


UITableView screenshot

你必须改变tableView:cellForRowAtIndexPath:一点点。

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    cell.textLabel.text = @"Name"; 
    cell.detailTextLabel.text = @"Jason"; 
    return cell; 
} 
+0

感谢你真是太棒了!那么,我甚至可以在IB中的表格视图中拖动或者只是拖动表格视图单元格?另外,我看不出你是如何将它限制在三排?我真的很感谢帮助! – Jason 2011-03-10 14:40:04

+0

只需抓住一个您可以找到的UITableView教程。 TableViews及其DataSource方法有点太复杂,无法在SO答案中解释它们。但有很多教程,因为其中大多数是初学者,你不会遇到大问题。如果您有任何问题,欢迎回到SO,并提出其他问题 – 2011-03-10 14:52:53

1

你不能“真的”在UITableView中创建列,但是你可以使用单元格样式来获得类似的效果。

可以使用UITableViewCellStyleValue1风格,实现你说的话 这里是个例:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 

cell.textLabel.text = @"Name"; 
cell.detailTextLabel.text = @"Jason"; 


return cell; 
} 

这里是什么样子的为例: http://blog.blackwhale.at/wp-content/uploads/2009/06/Bild-3.png

或者,但如果更复杂您是ObjectiveC新手,您可以在XIB中创建自己的CellView并在代码中使用它: http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/

Edi T:对不起,fluchtpunkt已经比我更快;)

+0

谢谢ingham!这个例子的链接正是我正在寻找的。我只是对Interface Builder中需要做什么感到困惑,我是否基本上重复你想要添加的每个单元格的示例代码? – Jason 2011-03-10 14:42:19

+0

@Jason你现在不应该阅读自定义uitableviewcell教程。在挖掘自定义单元格之前,您应该使用标准单元格进行几天的演练。 – 2011-03-10 14:54:11

1

也许你应该在表格单元格添加标签,并给予了边界

这样的:

// ------ -------------第一列----------------------------------- --------------------------------

UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 312, 43)]; 
[label1 setFont:[UIFont fontWithName:@"arial" size:18]]; 
[label1 setTextColor:[UIColor blackColor]]; 
label1.backgroundColor = [UIColor clearColor]; 
label1.layer.borderColor=[[UIColor lightGrayColor]CGColor]; 
label1.layer.borderWidth= 1.0f; 
label1.numberOfLines = 0; 
label1.text = [NSString stringWithFormat:@" %@",[[yourArray objectAtIndex:indexPath.row]objectForKey:@"xxxxx"]]; 
[cell.contentView addSubview:label1]; 

// --------- ----------第二列-------------------------------------- -----------------------------

UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(314, 0, 125, 43)]; 
[label2 setFont:[UIFont fontWithName:@"arial" size:18]]; 
[label2 setTextColor:[UIColor blackColor]]; 
label2.textAlignment = NSTextAlignmentCenter; 
label2.backgroundColor = [UIColor clearColor]; 
label2.layer.borderColor=[[UIColor lightGrayColor]CGColor]; 
label2.layer.borderWidth= 1.0f; 
label2.text = [NSString stringWithFormat:@"%@",[[yourArray objectAtIndex:indexPath.row]objectForKey:@"xxxxxx"]]; 
[cell.contentView addSubview:label2];