【问题标题】:UITableViewCell with UITableViewCellStyleValue1, adding new line to detailTextLabel at cell at bottom带有 UITableViewCellStyleValue1 的 UITableViewCell,在底部单元格的 detailTextLabel 中添加新行
【发布时间】:2014-02-11 15:49:17
【问题描述】:

在我的 tableview 上,我有最后一个单元格,它最初在第一张图片中不可见,当我向上滚动列表时,您可以在第二张图片中看到价格或我的 detailTextLabel 放在新行而不是维护正确的理由。
image 1 http://img706.imageshack.us/img706/4496/iphoneerror1.jpg

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


这是代码,我不知道为什么要这样做,任何方向或帮助将不胜感激

- (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;
}

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

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

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

    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];
    // .....
    

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

    【讨论】:

      【解决方案2】:

      您对所有行使用相同的单元格标识符,但第 0 行具有不同的样式。向上滚动时,可能会重复使用字幕样式的单元格。

      尝试为第 0 行使用不同的单元格标识符。仅将 cell 的声明保留在开关之外,并将 dequeueReusableCellWithIdentifier 移动到每个案例。

      【讨论】:

        【解决方案3】:
         (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        

        【讨论】:

        • 您能否在代码中添加一些注释来解释您做了什么以及为什么?在 SO 上,我们希望得到对当前用户和下一个看到您帖子的用户有帮助的实质性答案;这通过解释代码更容易实现。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-03
        相关资源
        最近更新 更多