开发者

Programmatically autoresizing UITableViewCell containing several subviews

开发者 https://www.devze.com 2023-01-22 07:33 出处:网络
I have a UITableView, which tries to look like a grid: that is I imitate four columns by adding UILabel**s as **cell.contentView subviews.

I have a UITableView, which tries to look like a grid: that is I imitate four columns by adding UILabel**s as **cell.contentView subviews. Three of these "columns" have fixed width, and on rotation I want to automatically resize the leftmost column to fill all available space (width of screen minus overall width of three other columns).

Cell initialization code is the following:

#define kCellWidth ((UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) ? 1024 : 768)
#define kCellHeight 60

#define kXOffset 10
#define kYOffset 10
#define kContentHeight (kCellHeight - 2*kYOffset)

#define kTypeWidth  125
#define kNumberWidth 100
#define kDateWidth  90
#define kLabelWidth  (kCellWidth - (2*kXOffset + kTypeWidth + kNumberWidth + kDateWidth))

#define kTypeOffset  (kXOffset + kLabelWidth)
#define kNumberOffset (kTypeOffset + kTypeWidth)
#define kDateOffset  (kNumberOffset + kNumberWidth)

#define kLabelTag 1
#define kTypeTag 2
#define kNumberTag 3
#define kDateTag 4

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

 if (cell == nil) {
  cell = [self cellWithFrame:CGRectMake(0, 0, kCellWidth, kCellHeight)
      identifier:kCellIdentifier];
 }

 Clazz *obj = [self.source objectAtIndex:indexPath.row];
 ((UILabel *) [cell viewWithTag:kLabelTag]).text  = obj.label;
 ((UILabel *) [cell viewWithTag:kTypeTag]).text  = obj.type;
 ((UILabel *) [cell viewWithTag:kNumberTag]).text = obj.number;
 ((UILabel *) [cell viewWithTag:kDateTag]).text  = obj.date;

 return cell;
}

-(UITableViewCell *) cellWithFrame:(CGRect)frame identifier:(NSString *)identifier {
 UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:frame
               reuseIdentifier:kCellIdentifier] autorelease];

 [cell.contentView addSubview:[self labelForTag:kLabelTag
           withFrame:CGRectMake(kXOffset, kYOffset, kLabelWidth, kContentHeight)]];
 [cell.contentView addSubview:[self labelForTag:kTypeTag
           withFrame:CGRectMake(kTypeOffset, kYOffset, kTypeWidth, kContentHeight)]];
 [cell.contentView addSubview:[self labelForTag:kNumberTag
           withFrame:CGRectMake(kNumberOffset, kYOffset, kNumberWidth, kContentHeight)]];
 [cell.contentView addSubview:[self labelForTag:kDateTag
           withFrame:CGRectMake(kDateOffset, kYOffset, kDateWidth, kContentHeight)]];

 return cell;
}

-(UILabel *)labelForTag:(int)tag withFrame:(CGRect)frame {
 UILabel *label = [[UILabel alloc] initWithFrame:frame];
 label.tag = tag;

 switch (tag) {
  case kNumberTag:
  case kDateTag:
   label.textAlignment = UITextAlignmentCenter;
   break;
  default:
   break;
 }

 return [label autorelease];
}

The questions are:

  • Can I achieve this type of behavior by playing with autoresizingMask and-or contentMode pr开发者_运维百科operty?
  • If yes, which objects' properties should I change and to which values?

I've experimented a bit, but since I'm an iPad-programming newbie, haven't been able to find the solution. Thanks in advance!


    CGRect frame = self.tableView.frame;
    CGFloat height1=120.0;
    [tableView setRowHeight:height1];
    self.tableView.frame = frame; 

This way you can set the rowheight programmatically

0

精彩评论

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