ios 5 中 tableView 中的自定义单元格

发布于 12-19 02:40 字数 1388 浏览 2 评论 0原文

我正在尝试减少特定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点2024-12-26 02:40:38

我不是专家,但我发现您正在使用两个不同的标识符。

如果我是你,我会尝试:

static NSString *CellIdentifier = @"name&email";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name&email"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    }

好吗?告诉我它是否有效:)

I'm not an expert, but I'm seeing that you are using two differents identifier.

If I were you I would try:

static NSString *CellIdentifier = @"name&email";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name&email"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    }

ok? Tell me if it works :)

他不在意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,这不会使其可见)。希望有帮助

If you have the custom cell created in Storyboard, check what the Identifier is and use that. The Identifier should be under the Attributes Inspector. If the Identifier is Cell, then your code should be something like this to get the Cell.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

If you have a UILabel called detail which you want to change the width of, you can change the size by changing the frame and reusing the previous frame origin and size height.

detail.frame = CGRectMake(detail.frame.origin.x, detail.frame.origin.y, newWidth,detail.frame.size.height);

Make sure you pass detail.size.height as the height, to keep the height the same (I noticed in your example that the height is 0 which wouldn't make it visible). Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文