Ios UITableView单元格重叠
我正在实现一个带有节索引的tableView。当任何特定部分中的行多于一行时,我的tableViewCells都会重叠 这是我的密码Ios UITableView单元格重叠,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我正在实现一个带有节索引的tableView。当任何特定部分中的行多于一行时,我的tableViewCells都会重叠 这是我的密码 ![- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return \[self.keys count\]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSIn
![- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return \[self.keys count\];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// return \[\[\[UILocalizedIndexedCollation currentCollation\] sectionTitles\] objectAtIndex:section\];
return \[keys objectAtIndex:section\];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// return \[\[UILocalizedIndexedCollation currentCollation\] sectionIndexTitles\];
return keys;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return \[\[UILocalizedIndexedCollation currentCollation\] sectionForSectionIndexTitleAtIndex:index\];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = \[tableView dequeueReusableCellWithIdentifier:nil\];
if (cell == nil) {
cell = \[\[UITableViewCell alloc\] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier\];
}
\[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)\];
UIView* bgview = \[\[UIView alloc\] initWithFrame:CGRectMake(0, 0, 1, 1)\];
bgview.opaque = YES;
bgview.backgroundColor = \[UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0\];
\[cell setBackgroundView:bgview\];
cell.textLabel.font = \[UIFont fontWithName:@"Roboto-Light" size:17\];
cell.detailTextLabel.font = \[UIFont fontWithName:@"Roboto-Light" size:13\];
cell.textLabel.backgroundColor = \[UIColor clearColor\];
cell.detailTextLabel.backgroundColor = \[UIColor clearColor\];
cell.textLabel.textColor = \[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\];
UIImageView *defaultImage = \[\[UIImageView alloc\]init\];
\[defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)\];
\[cell addSubview:defaultImage\];
for (int i=0; i<\[self.exhibitorArray count\]; i++) {
NSString * string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\];
NSString *firstLetter = \[string substringToIndex:1\];
if (\[\[keys objectAtIndex:indexPath.section\] isEqualToString:firstLetter\] ) {
NSString *urlString = \[\[NSString alloc\]init\];
urlString = @"http://";
urlString = \[urlString stringByAppendingString:\[NSString stringWithFormat:@"%@",\[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"image"\]\]\];
NSLog(@"%@",urlString);
AsyncImageView *asyncImageView = \[\[AsyncImageView alloc\] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)\];
\[asyncImageView setBackgroundColor:\[UIColor clearColor\]\];
NSString *tmp_string = \[\[NSString alloc\]init\];
tmp_string = urlString;
\[asyncImageView loadImageFromURL:\[NSURL URLWithString:tmp_string\]\];
\[defaultImage addSubview:asyncImageView\];
defaultImage.contentMode = UIViewContentModeScaleAspectFit;
tmp_string = nil;
asyncImageView = nil;
defaultImage = nil;
NSString *name_string = \[\[NSString alloc\]init\];
name_string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\];
NSLog(@"%@",name_string);
UILabel *user_name = \[\[ UILabel alloc\]init \];
\[user_name setFrame:(CGRectMake(58, 5, 270, 25))\];
\[user_name setBackgroundColor: \[UIColor clearColor\]\];
\[user_name setText:name_string\];
\[user_name setFont:\[UIFont fontWithName:@"Roboto-Light" size:14\]\];
\[user_name setTextAlignment:NSTextAlignmentLeft\];
\[user_name setTextColor:\[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\]\];
\[cell addSubview:user_name\];
user_name = nil;
}
}
\[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator\];
return cell;
}
![-(NSInteger)节数表视图:(UITableView*)表视图
{
返回\[self.keys count\];
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分{
//返回\[\[\[UILocalizedIndexedCollation currentCollation\]sectionTitles\]对象索引:section\];
返回\[键对象索引:节\];
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)表格视图{
//返回\[\[UILocalizedIndexedCollation currentCollation\]节索引\];
返回键;
}
-(NSInteger)tableView:(UITableView*)分区索引的tableView部分exttitle:(NSString*)标题索引:(NSInteger)索引{
返回\[\[UILocalizedIndexedCollation currentCollation\]Section for SectionIndexTextitLeatIndex:index\];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=\[tableView dequeueReusableCellWithIdentifier:nil\];
如果(单元格==nil){
cell=\[\[UITableViewCell alloc\]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier\];
}
\[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)\];
UIView*bgview=\[\[UIView alloc\]initWithFrame:CGRectMake(0,0,1,1)\];
bgview.opaque=是;
bgview.backgroundColor=\[UIColor color withred:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0\];
\[单元格设置背景视图:bgview\];
cell.textlab.font=\[UIFont fontWithName:@“Roboto Light”大小:17\];
cell.detailTextLabel.font=\[UIFont fontWithName:@“Roboto Light”大小:13\];
cell.textLabel.backgroundColor=\[UIColor clearColor\];
cell.detailTextLabel.backgroundColor=\[UIColor clearColor\];
cell.textlab.textColor=\[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\];
UIImageView*defaultImage=\[\[UIImageView alloc\]init\];
\[defaultImage setFrame:CGRectMake(5.0f、5.0f、40.0f、40.0f)\];
\[单元格添加子视图:默认图像\];
对于(int i=0;i更改行UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:nil];
withUITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.tag = 8888;
bgview.backgroundColor = [UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0];
[cell setBackgroundView:bgview];
cell.textLabel.font = [UIFont fontWithName:@"Roboto-Light" size:17];
cell.detailTextLabel.font = [UIFont fontWithName:@"Roboto-Light" size:13];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0];
UIImageView *defaultImage = [[UIImageView alloc]init];
[defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)];
defaultImage.contentMode = UIViewContentModeScaleAspectFit;
defaultImage.tag = 7777;
[cell.contentView addSubview:defaultImage];
UILabel *user_name = [[ UILabel alloc]init ];
user_name.tag = 9999;
[user_name setFrame:(CGRectMake(58, 5, 270, 25))];
[user_name setBackgroundColor: [UIColor clearColor]];
[user_name setFont:[UIFont fontWithName:@"Roboto-Light" size:14]];
[user_name setTextAlignment:NSTextAlignmentLeft];
[user_name setTextColor:[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0]];
[cell.contentView addSubview:user_name];
AsyncImageView *asyncImageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)];
asyncImageView.tag = 6666;
[asyncImageView setBackgroundColor:[UIColor clearColor]];
[defaultImage addSubview:asyncImageView];
}
[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImageView *defaultImage = (UIImageView*)[cell.contentView viewWithTag:7777];
UILabel *user_name = (UILabel*)[cell.contentView viewWithTag:9999];
AsyncImageView *asyncImageView = (AsyncImageView*)[defaultImage viewWithTag:6666];
for (int i=0; i<[self.exhibitorArray count]; i++) {
NSString * string = [[self.exhibitorArray objectAtIndex:i]valueForKey:@"name"];
NSString *firstLetter = [string substringToIndex:1];
if ([[keys objectAtIndex:indexPath.section] isEqualToString:firstLetter] ) {
NSString *urlString ;
urlString = @"http://";
urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"%@",[[self.exhibitorArray objectAtIndex:i]valueForKey:@"image"]]];
NSLog(@"%@",urlString);
NSString *tmp_string = urlString;
[asyncImageView loadImageFromURL:[NSURL URLWithString:tmp_string]];
NSString *name_string = [[self.exhibitorArray objectAtIndex:i]valueForKey:@"name"];
NSLog(@"%@",name_string);
[user_name setText:name_string];
}
}
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
UIView*bgview=[[UIView alloc]initWithFrame:CGRectMake(0,0,1,1)];
bgview.opaque=是;
bgview.tag=8888;
bgview.backgroundColor=[UIColor color withred:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0];
[cell setBackgroundView:bgview];
cell.textlab.font=[UIFont fontWithName:@“Roboto Light”大小:17];
cell.detailTextLabel.font=[UIFont fontWithName:@“Roboto Light”大小:13];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.textlab.textColor=[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0];
UIImageView*defaultImage=[[UIImageView alloc]init];
[默认图像设置帧:CGRectMake(5.0f、5.0f、40.0f、40.0f)];
defaultImage.contentMode=UIViewContentModeScaleAspectFit;
defaultImage.tag=7777;
[cell.contentView addSubview:defaultImage];
UILabel*用户名=[[UILabel alloc]init];
user_name.tag=9999;
[user_name setFrame:(CGRectMake(58,527025));
[用户名setBackgroundColor:[UIColor clearColor]];
[用户名setFont:[UIFont fontWithName:@“Roboto Light”大小:14];
[用户名setTextAlignment:NSTextAlignmentLeft];
[user_name setTextColor:[UIColor color with red:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0];
[cell.contentView addSubview:user_name];
AsyncImageView*AsyncImageView=[[AsyncImageView alloc]initWithFrame:CGRectMake(0.0f,0.0f,40.0f,40.0f)];
asyncImageView.tag=6666;
[asyncImageView setBackgroundColor:[UIColor clearColor]];
[defaultImage addSubview:asyncImageView];
}
[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
UIImageView*defaultImage=(UIImageView*)[cell.contentView视图带标记:7777];
UILabel*用户名=(UILabel*)[cell.contentView视图带标记:9999];
AsyncImageView*AsyncImageView=(AsyncImageView*)[defaultImage viewWithTag:6666];
对于(int i=0;i它们没有重叠。每次使用单元格时,您都会添加一个额外的标签
只需向单元格添加一个标签,然后重新使用该标签,而不是每次添加一个新标签
这同样适用于图像视图等
最好的解决方案是使用自定义UITableViewCell子类
可能的解决方案
注意:我不喜欢这个,因为使用了tag属性,但它可以在不子类化UITableviewCell的情况下完成
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
UIImageView *defaultImageView;
UILabel *customLabel;
if (cell == nil) {
// create the cell and empty views ready to take the content.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
defaultImageView = //create the image view
defaultImageView.tag = 3;
[cell.contentView addSubview:defaultImageView];
customLabel = //create custom label
customLabel.tag = 4;
[cell.contentView addSubview:customLabel];
} else {
// get the views that have already been created
defaultImageView = [cell.contentView viewWithTag:3];
customLabel = [cell.contentView viewWithTag:4];
}
// now populate the views...
defaultImageView.image = someImage;
customLabel.text = @"Hello, world";
return cell;
}
这样,您只需在单元格中创建一个标签和一个图像视图,然后正确地重用它。这只是因为您正在每个单元格上创建新标签数据源方法刚刚更改
只需alloc并添加标签即可
if (cell == nil) {
cell = \[\[UITableViewCell alloc\] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier\];
**//Allocate label here and set tag**
UILabel *nameLabel = [[UILabel alloc] initwithframe];
[nameLabel setTag:212];
}
**get label here by tag and set text on it here.**
eg
UILabel *nameLabel = [cell viewWithTag:212];
同样地,添加imageview内部条件并在外部设置图像,因为这一行已添加多次。要避免这种情况,请尝试以下操作:
UILabel *user_name = [[ UILabel alloc]init ];
user_name.tag = SomeTagValue;
.
.
.
.
UILabel *preView = [cell viewWithTag:SomeTagValue];
[preView removeFromSuperview];
[cell addSubview:user_name];
您正在添加两个cell.TextLabel
an
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = \[tableView dequeueReusableCellWithIdentifier:nil\];
if (cell == nil) {
cell = \[\[UITableViewCell alloc\] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier\];
}
\[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)\];
UIView* bgview = \[\[UIView alloc\] initWithFrame:CGRectMake(0, 0, 1, 1)\];
bgview.opaque = YES;
bgview.backgroundColor = \[UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0\];
\[cell setBackgroundView:bgview\];
UIImageView *defaultImage = \[\[UIImageView alloc\]init\];
\[defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)\];
\[cell addSubview:defaultImage\];
for (int i=0; i<\[self.exhibitorArray count\]; i++) {
NSString * string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\];
NSString *firstLetter = \[string substringToIndex:1\];
if (\[\[keys objectAtIndex:indexPath.section\] isEqualToString:firstLetter\] ) {
NSString *urlString = \[\[NSString alloc\]init\];
urlString = @"http://";
urlString = \[urlString stringByAppendingString:\[NSString stringWithFormat:@"%@",\[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"image"\]\]\];
NSLog(@"%@",urlString);
AsyncImageView *asyncImageView = \[\[AsyncImageView alloc\] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)\];
\[asyncImageView setBackgroundColor:\[UIColor clearColor\]\];
NSString *tmp_string = \[\[NSString alloc\]init\];
tmp_string = urlString;
\[asyncImageView loadImageFromURL:\[NSURL URLWithString:tmp_string\]\];
\[defaultImage addSubview:asyncImageView\];
defaultImage.contentMode = UIViewContentModeScaleAspectFit;
tmp_string = nil;
asyncImageView = nil;
defaultImage = nil;
NSString *name_string = \[\[NSString alloc\]init\];
name_string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\];
NSLog(@"%@",name_string);
UILabel *user_name = \[\[ UILabel alloc\]init \];
\[user_name setFrame:(CGRectMake(58, 5, 270, 25))\];
\[user_name setBackgroundColor: \[UIColor clearColor\]\];
\[user_name setText:name_string\];
\[user_name setFont:\[UIFont fontWithName:@"Roboto-Light" size:14\]\];
\[user_name setTextAlignment:NSTextAlignmentLeft\];
\[user_name setTextColor:\[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\]\];
\[cell addSubview:user_name\];
user_name = nil;
}
}
\[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator\];
return cell;
}