我在将 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 都被添加了不止一次。这是一个结果
任何帮助表示赞赏!