使用UITableViewCellStyleValue1的UITableViewCell,在底部的单元格中向detailTextLabel添加新行 [英] UITableViewCell with UITableViewCellStyleValue1, adding new line to detailTextLabel at cell at bottom

查看:97
本文介绍了使用UITableViewCellStyleValue1的UITableViewCell,在底部的单元格中向detailTextLabel添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的tableview上我有最后一个单元格,在第一张图片中看不到最初可见,当我向上滚动列表时,你可以在第二张图片中看到价格或我的detailTextLabel放在一个新行上保持正确的理由。


image 1 http://img706.imageshack .us / img706 / 4496 / iphoneerror1.jpg

on my tableview i have the last cell that is not initially visible as seen in the first image, when i scroll the list up, you can see in the second image that the price or my detailTextLabel is put on a new line not maintaining the right justification.
image 1 http://img706.imageshack.us/img706/4496/iphoneerror1.jpg

image 2 http://img706.imageshack.us/img706/6007/iphoneerror2edited.jpg



这是代码,我无法弄清楚为什么这样做,任何方向或帮助都会非常感激


Here is the code, i can't figure out why its doing this, any direction or help would be much appreciated

- (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:CellIdentifier];    
    // Configure the cell.
    NSUInteger indexRow = [indexPath row];
    switch (indexRow) {
        case 0:{
            NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
            NSString *description = [[currentData objectForKey:@"Description"] stringByTrimmingCharactersInSet:set];

            if (cell == nil) {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            }
            cell.selectionStyle =  UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;
            cellShift = 1;


            if (![description isEqualToString:@""]) {
                cell.textLabel.text = @"";
                cell.detailTextLabel.text = description;
                cell.detailTextLabel.numberOfLines = 2;
            }
            else {
                cell.textLabel.text = @"";
                cell.detailTextLabel.text = @"";
                cell.detailTextLabel.numberOfLines = 0;


            }
            break;
        }
        default:{
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
            }
            NSDictionary *item = [tableData objectAtIndex:(indexRow-cellShift)];
            NSString *name = [item objectForKey:@"Name"];
            if ([name length] > MaxVendorsLength ) {
                name =  [NSString stringWithFormat:@"%@ ...",[name substringToIndex:MaxVendorsLength]];
            }
            cell.textLabel.text = name;
            cell.textLabel.minimumFontSize = 12;

            NSString *priceString;
            float price = [[item objectForKey:@"Price"] floatValue];
            //NSLog(@"| %@ | : | %@ |",[item objectForKey:@"Name"], [item objectForKey:@"Price"]);
            if (price != 0) {
                priceString = [[NSString alloc] initWithFormat:@"$%.2f",price];
            }
            else {
                priceString = [[NSString alloc] initWithString:@"--"];
            }

            cell.detailTextLabel.text =  priceString;

            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            [priceString release];          
            break;
        }
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.textLabel.minimumFontSize = 14;
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.detailTextLabel.minimumFontSize = 14;
    return cell;
}

如果我需要发布任何其他内容以获取帮助,请告诉我们? ??

Let me know if i need to post anything else to get help with this ???

推荐答案

这是因为当您向上滚动时,顶部单元格会被重复使用,因为所有单元格都具有相同的单元格标识符(你有第一行)。您需要声明两个单元格标识符,并根据您尝试获取的行使用相应的标识符。

It's because when you scroll up the top cell is reused because all of your cells have the same cell identifier (the first line you have). You need to declare the two cell identifiers and use the appropriate one based on what row you're trying to get.

static NSString *FirstRowCellIdentifier = @"A";
static NSString *OtherRowCellIdentifier = @"B";

NSString *cellIdentifier = nil;
if ([indexPath row] == 0)
    cellIdentifier = FirstRowCellIdentifier;
else
    cellIdentifer = OtherRowCellIdentifier;

UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:cellIdentifier];
// .....

然后您可以使用其余代码作为是。这只是确保重用的单元格类型正确。

Then you can use the rest of your code as is. This just ensures that the resused cell is of the correct type.

这篇关于使用UITableViewCellStyleValue1的UITableViewCell,在底部的单元格中向detailTextLabel添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆