防止自定义UITableViewCell重复

时间:2012-07-03 14:30:02

标签: objective-c uitableview

我有一个带有WishInfoTextView的自定义UITableViewCell,它只是UILabel的一个子类。

 static NSString *CellIdentifier = @"Cell";
 NSMutableDictionary* item = [myWishes objectAtIndex: indexPath.row];

WishTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if( cell == nil ) {
    cell = [[WishTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [cell setSelectionStyle: UITableViewCellEditingStyleNone];
    [cell setWishInfo: item];
}    

WishInfoTextView* infoText = (WishInfoTextView*)[cell viewWithTag: kTableInfoText];
infoText.text = [item objectForKey:@"name"];

NSLog(@"\nTaggedText: %@\nNormalText: %@", infoText.wishName, [item objectForKey:@"name"]);

唯一的问题是我有重复的细胞。我读了一些关于设置标签的内容 但如果我通过他的标签访问我的标签,没有任何变化。

1 个答案:

答案 0 :(得分:1)

每次想要一个小区时,你都在使用objectForKey@"name"。所以你总会得到同样的东西。如果您有一个字符串数组,请使用类似[arrayName objectAtIndex:indexPath.row]的内容,以便实际获得不同的值。

例如,当我从数组填充表时,我使用

NSArray *nameSection = [appD.data objectForKey:key];
cell.textLabel.text = [nameSection objectAtIndex:indexPath.row];

appD.data来自

NSString *DataPath = [Path stringByAppendingPathComponent:@"Primary.plist"];
NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:DataPath];

Primary.plist

的位置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>First type</key>
    <array>
        <string>object1</string>
        <string>object2</string>
        <string>object3</string>
    </array>
    <key>Second type</key>
    <array>
        <string>thing1</string>
        <string>thing2</string>
        <string>thing3</string>
    </array>
</dict>
</plist>
相关问题