开发者

Table View with two "columns"?

开发者 https://www.devze.com 2023-02-15 20:30 出处:网络
I\'m really new to Xcode and Objective C but I can\'t seem to find a tutorial on a simple table I\'d like to create.I\'d like to have开发者_C百科 a table (grouped style) with only three rows but two c

I'm really new to Xcode and Objective C but I can't seem to find a tutorial on a simple table I'd like to create. I'd like to have开发者_C百科 a table (grouped style) with only three rows but two columns. Column A would have a label (like 'Name:') and column B would have the actual data ("Jason").

I can't seem to find how to do this. Unless I'm just completely looking up the wrong thing? Was hoping someone could help me on how to do this or point me in the right direction.

Thank you!!


You don't want to use a table with 2 columns. They are not used in iOS.
You should use a UITableViewCell with a UITableViewCellStyleValue2 style. And you want to set @"Name" as textLabel.text and @"Jason" as detailTextLabel.text.


Table View with two "columns"?

You have to change the tableView:cellForRowAtIndexPath: a little bit.

// 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;
}


You can't "really" create columns in UITableView, but you can use Cell Styles to achieve a similar effect.

You can use the UITableViewCellStyleValue1 style to achieve what you say Here is an exemple:

- (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;
}

Here is an exemple of what it looks like: http://blog.blackwhale.at/wp-content/uploads/2009/06/Bild-3.png

Or, but more complicated if you are new to ObjectiveC, you can create your own CellView in a XIB and use it in your code: http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/

Edit: Sorry, fluchtpunkt has been faster than me ;)


maybe you should add a label on the table cell, and give a border

like this :

//------------------- first column -------------------------------------------------------------------

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];

//------------------- second column -------------------------------------------------------------------

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消