Reputation: 2512
I'm implementing a tableView with section index. where my tableViewCells are overlapping whenever the rows are more than one in any particular section.
Here is my code.
![- (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;
}
Upvotes: 2
Views: 6265
Reputation: 77621
They're not overlapping. You are adding an additional label each time the cell is used.
You need to only add one label to a cell and then reuse that label instead of adding a new one each time.
The same is true for the image views etc...
You're best solution is to use a custom UITableViewCell subclass... http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Possible solution
Note: I don't like this because of the use of the tag property but it will get it done without subclassing 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;
}
This way you are only creating one label and one image view in the cell and then reusing it propelry.
Upvotes: 7
Reputation: 11197
You are adding both cell.TextLabel
and another Label (Named: user_name
). If you want a custom table cell with imageview and label you can see this.
As you can see in your output that label is overlapping. Its not the same label these are different labels in a cell. Hope this helps.. :)
Edit: Try this:
- (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;
}
Upvotes: 0
Reputation: 3506
Change the line UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
with UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
Edit
- (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;
}
Upvotes: 0
Reputation: 8105
Because of this line [cell addSubview:user_name];
It've added multiple times.To avoid this, try below:
UILabel *user_name = [[ UILabel alloc]init ];
user_name.tag = SomeTagValue;
.
.
.
.
UILabel *preView = [cell viewWithTag:SomeTagValue];
[preView removeFromSuperview];
[cell addSubview:user_name];
Upvotes: 0
Reputation: 3529
Thats just because you are creating new labels on every cell datasource method just change
just alloc and add label in
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];
Similarly add imageview inside condition and set images on that outside
Upvotes: 0