我试图在labels
上添加tableView
(左侧和右侧)。但是,label texts
被削减,我想知道如何解决。我正在添加我的源代码以及附加屏幕捕获。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
cell.textLabel.text = [[dataArray objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.detailTextLabel.text= [NSString stringWithFormat:@"$ %@", [[dataArray objectAtIndex:indexPath.row] objectForKey:@"price"]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0;
}
答案 0 :(得分:2)
你可以用两种方式做到这一点
您需要将文本对齐更改为右侧,例如
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
或在Right Detail
XCode
将您的UITableViewCellStyleDefault
更改为UITableViewCellStyleValue1
或UITableViewCellStyleValue2
您可以在UITableViewCell class
并调用
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text= @"Yesterday";
有关详细信息,您可以获取示例here否则创建自定义单元格并根据您的需要将标签设置为右对齐
答案 1 :(得分:0)
更改以下代码行:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
试试这个:
- (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];
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text= @"Yesterday";
return cell;
}
希望它有所帮助!
答案 2 :(得分:0)
应该是这样的
- (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];
}
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.detailTextLabel.text= @"Yesterday";
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.font=[UIFont fontWithName:@"Arial" size:12];//If you want decrease font size here
cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
return cell;
}
答案 3 :(得分:0)
您可以使用NSLineBreakByCharWrapping
包装标签
在此处查看此代码myLabelWidth是您的标签宽度,myLabelHeight是您的标签高度
CGSize size = [textLabel.text sizeWithAttributes:
@{NSFontAttributeName:
[UIFont systemFontOfSize:17.0f]}];
int sizeofL=size.width;
if(sizeofL>(mylabelWidth))
{
[textLabel setMinimumFontSize:5.0];
[textLabel setNumberOfLines:0];
int hgtofL=size.height;
if(hgtofL>mylabelHieght)
{
if(height<1024)
textLabel.font = [UIFont systemFontOfSize:12];
else
textLabel.font = [UIFont systemFontOfSize:14];
}
else
{
if(height<1024)
textLabel.font = [UIFont systemFontOfSize:16];
else
textLabel.font = [UIFont systemFontOfSize:18];
}
textLabel.lineBreakMode = NSLineBreakByCharWrapping;
[textLabel sizeToFit];
}
else
{
if(height<1024)
textLabel.font = [UIFont systemFontOfSize:16];
else
textLabel.font = [UIFont systemFontOfSize:18];
}
cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
[cell.contentView addSubview:textLabel];
[cell setEditing:NO];
}