【发布时间】:2012-11-23 07:53:16
【问题描述】:
我对 iphone 开发非常陌生,并且在某一点上感到震惊。实际上我的需要是我在UITableView 中显示一些结果,然后我正在显示UILables 的UITableView's 单元格..
如何使UILabel's 根据文本调整内容。实际上文本不是静态的。它可能会在运行时发生变化。所以我需要将动态大小设置为 UILabel 。其次假设文本是 null 那么它不应该显示任何空格,并且下一个 UILabel 应该开始。我怎样才能使它成为可能?
这是我的代码..
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,320,800) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentHtmlElement==@"3")
{
static NSString *CellIdentifier=@"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSMutableDictionary *d = (NSMutableDictionary *) [arr2 objectAtIndex:indexPath.row];
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
UILabel *name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 40)];
name1.font=[UIFont boldSystemFontOfSize:16];
[name1 setTextAlignment:UITextAlignmentLeft];
[name1 setText:[d valueForKey:@"Name"]];
name1.tag = 111;
[cell addSubview:name1];
[name1 release];
UILabel *codetype=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
codetype .font=[UIFont boldSystemFontOfSize:12];
[codetype setTextAlignment:UITextAlignmentLeft];
[codetype setText:[d valueForKey:@"Code"]];
codetype.tag=112;
[cell addSubview:codetype];
[codetype release];
UILabel *id=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
id .font=[UIFont fontWithName:@"arial" size:12];
[id setTextAlignment:UITextAlignmentLeft];
[id setText:[d valueForKey:@"ID"]];
id.tag=113;
[cell id ];
[id release];
UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320,10)];
address .font=[UIFont fontWithName:@"arial" size:12];
[address setTextAlignment:UITextAlignmentLeft];
[address setText:[d valueForKey:@"address"]];
line2.tag=114;
[cell addSubview: address];
[address release];
UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 320, 10)];
city .font=[UIFont fontWithName:@"arial" size:12];
[city setTextAlignment:UITextAlignmentLeft];
[city setText:[d valueForKey:@"City"]];
city.tag=115;
[cell addSubview:city ];
[city release];
}
在这里,我将所有 UILabel 设置为静态大小。但我需要的是动态大小。假设如果地址标签为空,那么它不应该显示任何空格和下一个标签,即城市应该在 id 之后立即开始。这怎么可能?
【问题讨论】:
-
据我所知...您的文字在标签中重叠...
-
你为什么不使用可重复使用的单元格..?
-
我真正需要的是第一个标签,我会给出固定尺寸。从第二个标签,标签应该根据前一个标签的内容自动设置它的尺寸(y轴和高度)。如果假设,如果一个标签的值为空,那么该标签应该消失,并且下一个标签应该立即开始。我该怎么做?
-
这与 Xcode 没有任何关系。
-
你应该只在stackoverflow上使用英文
标签: iphone ios cocoa-touch uitableview