ios 5 中 tableView 中的自定义单元格
我正在尝试减少特定 indexPath.row 处单元格的一个标签的宽度,但它不起作用。 下面我添加了代码,并且我正在使用 iOs 5 故事板。 有没有其他方法可以在运行时更改标签的大小?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name&email"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
UILabel *detail = (UILabel *)[cell viewWithTag:101];
if(indexPath.row == 0){
nameLabel.text = @"Name : ";
detail.text = @"";
}else if(indexPath.row == 1){
nameLabel.text = @"Email : ";
detail.text = @"";
}else if(indexPath.row == 2){
nameLabel.text = @"Location Alerts";
detail.text = @"Notifies people when they are close to a saved spot";
// here i am changing the width of lable
detail.frame = CGRectMake(0, 0, 100, 0);
}else if(indexPath.row == 3){
nameLabel.text = @"Share This App :";
detail.text = @"(post to facebook/twitter)";
}else if(indexPath.row == 4){
nameLabel.text = @"Review This App :";
detail.text = @"(on iTunes)";
}else if(indexPath.row == 5){
nameLabel.text = @"Get Help :";
detail.text = @"(send an email to me)";
}
// Configure the cell...
return cell;
}
I am trying to reduce the cell's one label's width at particular indexPath.row but its not working..
below i added code for that and I am using iOs 5 storyboard.
Any one have other way to change the size of label at run time ??
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name&email"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
UILabel *detail = (UILabel *)[cell viewWithTag:101];
if(indexPath.row == 0){
nameLabel.text = @"Name : ";
detail.text = @"";
}else if(indexPath.row == 1){
nameLabel.text = @"Email : ";
detail.text = @"";
}else if(indexPath.row == 2){
nameLabel.text = @"Location Alerts";
detail.text = @"Notifies people when they are close to a saved spot";
// here i am changing the width of lable
detail.frame = CGRectMake(0, 0, 100, 0);
}else if(indexPath.row == 3){
nameLabel.text = @"Share This App :";
detail.text = @"(post to facebook/twitter)";
}else if(indexPath.row == 4){
nameLabel.text = @"Review This App :";
detail.text = @"(on iTunes)";
}else if(indexPath.row == 5){
nameLabel.text = @"Get Help :";
detail.text = @"(send an email to me)";
}
// Configure the cell...
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
他不在意2024-12-26 02:40:38
如果您在 Storyboard 中创建了自定义单元格,请检查标识符是什么并使用它。 标识符应该位于属性检查器下。如果标识符是单元格,那么您的代码应该像这样来获取单元格。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
如果您有一个名为 detail 的 UILabel,您想要更改其宽度,则可以通过更改 frame 并重用之前的框架来更改 size 框架原点和尺寸高度。
detail.frame = CGRectMake(detail.frame.origin.x, detail.frame.origin.y, newWidth,detail.frame.size.height);
确保将 detail.size.height
作为高度传递,以保持高度相同(我注意到在您的示例中高度为 0,这不会使其可见)。希望有帮助
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我不是专家,但我发现您正在使用两个不同的标识符。
如果我是你,我会尝试:
好吗?告诉我它是否有效:)
I'm not an expert, but I'm seeing that you are using two differents identifier.
If I were you I would try:
ok? Tell me if it works :)