我在viewDidLoad上创建以下数据:

- (void)viewDidLoad {
    [super viewDidLoad];

    food = [[NSMutableArray alloc] initWithObjects: @"Harvey's",@"McCormicks",@"Subway",nil];
    gifts = [[NSMutableArray alloc] initWithObjects:@"The gift Shop",@"Lilo Gifts",@"Prezzies for All", nil];
    services = [[NSMutableArray alloc] initWithObjects:@"Hair Snippers",@"Laundro-mat",@"Accountants",@"Shoe fitters", nil];
    hotel = [[NSMutableArray alloc] initWithObjects:@"Hotel Paradiso",@"Dick & Doms B&B",@"Super Hotel",@"Castle B&B", nil];
    pubs = [[NSMutableArray alloc] initWithObjects:@"The Snuggly Duckling",@"Beer's Arms",@"Merlins Beard", @"Cheers", nil];


}

这是我的数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 5;
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return NSLocalizedString(@"Food & Restaraunts", nil);
        case 1:
            return NSLocalizedString(@"Shopping/ Gifts", nil);
        case 2:
            return NSLocalizedString(@"Services", nil);
        case 3:
            return NSLocalizedString(@"Hotels / B&B's", nil);
        case 4:
            return NSLocalizedString(@"Pubs",nil);

        default:
            return nil;
    }



}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 3;
        case 1:
            return 3;
        case 2:
            return 4;
        case 3:
            return 4;
        case 4:
            return 4;
        //default:
        //  return 1;

    }



}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    //mine  

        switch (indexPath.section) {
    case 0:
        [[ cell textLabel]
         setText:[food objectAtIndex:indexPath.row]];
        break;
    case 1:
        [[ cell textLabel]
         setText:[gifts objectAtIndex:indexPath.row]];
        break;
    case 2:
        [[ cell textLabel]
        setText:[services objectAtIndex:indexPath.row]];
        break;
    case 3:
        [[ cell textLabel]
        setText:[hotel objectAtIndex:indexPath.row]];
        break;
    case 4:
        [[ cell textLabel]
        setText:[pubs objectAtIndex:indexPath.row]];
        break;

    } 

    return cell;
}

}

它的实际作用是正确显示前10行,然后再次重复该列表,如果我在前几节中添加更多行,它将在表的前面重复。.我已经清除了缓存等,但是没有任何效果。

===============>>#1 票数:1 已采纳

这是因为您只设置了可重复使用单元格的内容一次。

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];}
//mine  

    switch (indexPath.section) {
case 0:
    [[ cell textLabel]
     setText:[food objectAtIndex:indexPath.row]];
    break;
case 1:
    [[ cell textLabel]
     setText:[gifts objectAtIndex:indexPath.row]];
    break;
case 2:
    [[ cell textLabel]
    setText:[services objectAtIndex:indexPath.row]];
    break;
case 3:
    [[ cell textLabel]
    setText:[hotel objectAtIndex:indexPath.row]];
    break;
case 4:
    [[ cell textLabel]
    setText:[pubs objectAtIndex:indexPath.row]];
    break;



return cell;}

我不擅长后期格式化。 从“重用堆栈”中获取单元格后,应设置其内容。

  ask by Craig translate from so

未解决问题?本站智能推荐: