0

I have already made a custom class with the init:

if(self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]) {
}

As I wanted to add a blue texted label to the right hand side of the cell and have it go right when selected otherwise it looked odd when highlighted:

self.sizeTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width-110, 0.0, 100.0, 44.0)];
self.sizeTextLabel.textAlignment = NSTextAlignmentRight;
self.sizeTextLabel.backgroundColor = [UIColor whiteColor];
self.sizeTextLabel.textColor = [UIColor colorWithRed:81/255.0 green:102/255.0 blue:145/255.0 alpha:1.0];
self.sizeTextLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:self.sizeTextLabel];

There is probably a better way of doing that ^

What I would like to do is add another label that is effectively another subtitle row, I then increase the height of the row when the cell is created with the heightForRowAtIndexpath method.

Problem: When I add a new label 'row' to the content view, it does not get higher (the default views shift to the middle of the view). How do I create and position it correctly below the subtitle view? If I were to change the first subtitle to multiline it would be nice if the second label knew what to do.

I wish cocoa had relative positioning. Or I haven't found it yet!

4

2 回答 2

2

您可以将详细文本标签设为多行并将两个字符串添加到一个标签中。在此示例中,我的数据在其字典中有三个键,“main”用于主标签文本,“detail1”和“detail2”用于两个字幕字符串。

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

    NSDictionary *object = _objects[indexPath.row];
    cell.textLabel.text = object[@"main"];
    NSString *concat = [NSString stringWithFormat:@"%@\n%@",object[@"detail1"],object[@"detail2"]];
    cell.detailTextLabel.numberOfLines = 2;
    cell.detailTextLabel.text = concat;
    return cell;
}
于 2012-12-11T17:03:11.200 回答
0

您可以使用界面生成器制作自定义单元格并使用它来代替 UITableViewCell。

检查这个:http ://www.backslashtraining.com/blog/2012/3/10/ios-5-tip-of-the-week-custom-table-view-cells.html

于 2012-12-11T15:57:17.990 回答