2

UILabel をサブビューとして UITableViewCell に追加する際に問題があります。私のコード:

bookMarkTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 50, 480, 270)];
    bookMarkTable.rowHeight = 38.0f;
    bookMarkTable.backgroundColor=[UIColor colorWithRed:247/255.0 green:246/255.0 blue:242/255.0 alpha:1];
    [bookMarkTable setDelegate:self];
    [bookMarkTable setDataSource:self];
    [bookMarkMainView addSubview:bookMarkTable];

および UITableView デリゲート メソッド:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return bookMarksArray.count;
}

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

    }

    // Configure the cell...

    NSMutableDictionary* cellDictionary =[bookMarksArray objectAtIndex:indexPath.row];

    UILabel* bookMarkTitle =[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 370, 40)];
    bookMarkTitle.text=[cellDictionary valueForKey:@"bm_title"];
    bookMarkTitle.backgroundColor=[UIColor clearColor];
    bookMarkTitle.font=[UIFont fontWithName:@"Georgia" size:20.0f];
    [cell.contentView addSubview:bookMarkTitle];
    return cell;

}
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath{
    return 38.0f;
}

このメソッドで行を追加します

-(void)addBookmark{
    NSMutableDictionary* infoDict=[[NSMutableDictionary alloc]init];
    NSString* bookmarkTitle=[NSString stringWithFormat:@"This is :[%i]",bookMarksArray.count];
    [infoDict setValue:bookmarkTitle forKey:@"bm_title"];
    [bookMarksArray addObject:infoDict];
    [bookMarkTable reloadData];

}

しかし、このメソッドを複数回呼び出すと、すべての UILabel が複数回追加されているようです。これが結果です結果

どんな助けでも大歓迎です!

4

3 に答える 3

0

最初に- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath、サブビューを削除する必要があります。

for(UIView* vista in cell.contentView){
    [vista removeFromSuperview];
}

それだけです。

PD: 削除されたビューが正しいかどうかを確認します。

于 2013-10-02T19:32:43.800 に答える