Табличное представление с двумя "столбцами"? - PullRequest
0 голосов
/ 10 марта 2011

Я действительно новичок в Xcode и Objective C, но не могу найти учебник по простой таблице, которую я хотел бы создать.Я хотел бы иметь таблицу (сгруппированный стиль) только с тремя строками, но двумя столбцами.Столбец A будет иметь метку (например, «Имя:»), а столбец B будет содержать фактические данные («Джейсон»).Разве я просто полностью искал не ту вещь?Я надеялся, что кто-нибудь может мне помочь, как это сделать или указать мне правильное направление.

Спасибо !!

Ответы [ 3 ]

7 голосов
/ 10 марта 2011

Вы не хотите использовать таблицу с 2 столбцами. Они не используются в iOS.
Вы должны использовать UITableViewCell со стилем UITableViewCellStyleValue2. И вы хотите установить @"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;
}
1 голос
/ 26 июля 2013

возможно, вам следует добавить метку в ячейку таблицы и поставить рамку

как это:

// ------------------- первый столбец ------------------------ -------------------------------------------

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];
1 голос
/ 10 марта 2011

Вы не можете «реально» создавать столбцы в 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, вы можете создать свой собственный CellView в XIB и использовать его в своем коде: http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/

Редактировать: Извините, fluchtpunkt был быстрее меня;)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...