I'm making UITableView without IB.
My UITableView has 2 styles UITableViewCell - first row style and other row style.
I use MTLable Class instead of UILabel.
Question:
This code's results is very strange.
First cell style and other cell style is mixed when next page scrolled.
I can't find my code's faults.
I need your advice. Thanks!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//firsT cELL
if(indexPath.row==0)
{
CGRect frame=CGRectMake(0,0,80, 60);
MTLabel *label1=[[MTLabel alloc] init];
label1.backgroundColor = [UIColor grayColor];
label1.frame=frame;
label1.text=#"123";
label1.tag = 1001;
[cell.contentView addSubview:label1];
label1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin &UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleLeftMargin & UIViewAutoresizingFlexibleRightMargin;
label1.contentMode = UIViewContentModeTopLeft;
[label1 setTextAlignment:MTLabelTextAlignmentRight];
[label1 release];
}
else
{
CGRect frame3=CGRectMake(0,0,80, 60);
MTLabel *label3=[[MTLabel alloc]init];
label3.frame=frame3;
label3.backgroundColor = [UIColor purpleColor];
label3.text=#"100";
label3.tag = 1003;
label3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin &UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleLeftMargin & UIViewAutoresizingFlexibleRightMargin;
label3.contentMode = UIViewContentModeTopLeft;
[label3 setTextAlignment:MTLabelTextAlignmentRight];
[cell.contentView addSubview:label3];
[label3 release];
}
}
FTS_book *book = [items objectAtIndex:indexPath.row];
if(indexPath.row==0)
{
MTLabel *label1 = (MTLabel*)[cell viewWithTag:1001];
label1.text= [NSString stringWithFormat:#"%d", book.chapter];
}
else
{
MTLabel *label3 = (MTLabel*)[cell viewWithTag:1003];
label3.text= [NSString stringWithFormat:#"%d", book.verse];
}
return cell;
}
Use different cell Id's for those two styles.
Related
I am working on a tvOS app in which custom table view cell is used, while showing data using labels is working perfectly fine but button selection is not working. is there any way to make button selection inside custom table view cell work in tvOS table view?
code snippet:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MsgDetailsTableViewCell *cell;
static NSString *simpleTableIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MsgDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary*dataDict = [self.qArray objectAtIndex:indexPath.row];
NSString *followCountText = [NSString stringWithFormat:#"%# Following",[[dataDict objectForKey:#"followCount"] stringValue]];
if([[dataDict objectForKey:#"followCount"] integerValue]!=0)
{
[cell.statusListBtn addTarget:self action:#selector(openLikedList:) forControlEvents:UIControlEventPrimaryActionTriggered];
cell.statusListBtn.tag=111;
cell.followCountLbl.textColor = [UIColor colorWithRed:0/255.0 green:95/255.0 blue:167/255.0 alpha:1.0];
}
else
{
[cell.statusListBtn removeTarget:self action:#selector(openLikedList:) forControlEvents:UIControlEventPrimaryActionTriggered];
cell.statusListBtn.tag=111;
cell.followCountLbl.textColor = [UIColor grayColor];
}
cell.followCountLbl.text=followCountText;
return cell;
}
In above code Selection of statusListBtn is not working.
Do you mean that only when dataDict objectForKey:#"followCount"] integerValue]!=0 the button can perform the selector if so. you can try this
MsgDetailsTableViewCell *cell;
static NSString *simpleTableIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MsgDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
[cell.statusListBtn addTarget:self action:#selector(openLikedList:) forControlEvents:UIControlEventPrimaryActionTriggered];
cell.statusListBtn.tag=111;
}
NSDictionary*dataDict = [self.qArray objectAtIndex:indexPath.row];
NSString *followCountText = [NSString stringWithFormat:#"%# Following",[[dataDict objectForKey:#"followCount"] stringValue]];
if([[dataDict objectForKey:#"followCount"] integerValue]!=0)
{
cell.statusListBtn.userInteractionEnabled = YES;
cell.followCountLbl.textColor = [UIColor colorWithRed:0/255.0 green:95/255.0 blue:167/255.0 alpha:1.0];
}
else
{
cell.statusListBtn.userInteractionEnabled = NO;
cell.followCountLbl.textColor = [UIColor grayColor];
}
cell.followCountLbl.text=followCountText;
return cell;
If you want different event of button you can change UIControlEventPrimaryActionTriggered like UIControlEventTouchUpInside
I have a UITableView with different sections and rows. I want to have different label or image like a icone close each cell but I don't know why it's looks like this picture:
Also scrolling changed the size of labels!
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:label];
}
else if(indexPath.row == 1)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else if(indexPath.row == 2)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else{
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
In your code when user scroll table, labels created many times! You must check if color label already exist in cell and then set color
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int colorLabelTag = 9999;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Because table cells are reusing we must check is label already in cell
UILabel* label = (UILabel *) [cell viewWithTag:colorLabelTag];
// If no - we create label
if (!label) {
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.tag = colorLabelTag;
[cell addSubview:label];
}
if(indexPath.row == 0)
{
label.backgroundColor = [UIColor darkGrayColor];
}
else if(indexPath.row == 1)
{
label.backgroundColor = [UIColor redColor];
}
else if(indexPath.row == 2)
{
label.backgroundColor = [UIColor redColor];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
Unless you want more cusotomizing than this there is no need for subclassing the cell.
Let me give it a try:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
// If you need to add more custom views then create them here. E.g.:
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10,200,20,20)];
myLable.text = #"Some Value";
label.tag = 101; // use anything but 1. Better use a constant expression.
[cell.contentView addSubview:myLable];
}
switch (indexPath.row) {
case 1:
cell.imageView.image = [UIImage imageNamed:#"red.png"];
break;
case 2:
cell.imageView.image = [UIImage imageNamed:#"blue.png"];
break;
case 3:
cell.imageView.image = [UIImage imageNamed:#"green.png"];
break;
}
UILabel* theLabel = (UILabel *) [cell viewWithTag:101]; // again, a constant is of better style here.
if (theLabel { // it must exist. However, it is good practice to check upon that.
label.text = #"someOtherValue";
}
[[cell textLabel] setText:contentForThisRow];
// you may need to layout your subviews here.
return cell;
}
I have created a UITableView with two cells, username and password using the code below. I am doing this programatically as static cells can't be used in a UIViewController (where this table must reside)
I have included a screenshot of the output the first time the view loads (the expected output) and the second time the view is shown (the incorrect output)
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if( cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Login Ident"];
if (indexPath.row == 0) {
loginId = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
loginId .placeholder = #"Email address";
loginId .autocorrectionType = UITextAutocorrectionTypeNo;
[loginId setClearButtonMode:UITextFieldViewModeWhileEditing];
cell.accessoryView = loginId;
}
if (indexPath.row == 1) {
password = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
password.placeholder = #"Password";
password.secureTextEntry = YES;
password.autocorrectionType = UITextAutocorrectionTypeNo;
[password setClearButtonMode:UITextFieldViewModeWhileEditing];
cell.accessoryView = password;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
loginId.delegate = self;
password.delegate = self;
[loginId setText:[keychain objectForKey:(__bridge id)kSecAttrAccount]];
[password setText:[keychain objectForKey:(__bridge id)kSecValueData]];
[loginId addTarget:self
action:#selector(textFieldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[password addTarget:self
action:#selector(textFieldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview:loginId];
[cell.contentView addSubview:password];
//[_tableView addSubview:loginId];
//[_tableView addSubview:password];
}
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_tableView reloadData];
}
My UITextField declarations
UITextField *loginId;
UITextField *password;
If anybody is able to help me out with this, I'd really appreciate it.
Looks like you are having issues with your cell reuse.
Here is how I would do it:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Login Ident";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:identifier];
if (nil == cell) {
// Within this if statement do any work that is not going to change over the lifetime of the cell.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// You was setting `selectionStyle` on every run so it should be
// safe to set here once
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
const NSInteger textFieldTag = 10; // <- No special meaning, just non-zero
UITextField *textField = (UITextField *)[cell viewWithTag:textFieldTag];
// I've followed a similar pattern to the cell reuse above for creating the textfield
// This ensures that we only create a textfield once per cell and then reuse it
if (nil == textField) {
textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
textField .autocorrectionType = UITextAutocorrectionTypeNo;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self;
textField.tag = textFieldTag;
[cell addSubview:textField];
}
// Next do any work to configure the different cells
if (indexPath.row == 0) {
textField.placeholder = #"Email address";
cell.accessoryView = loginId;
textField = textField;
loginId = textField;
} else if (indexPath.row == 1) {
textField.placeholder = #"Password";
textField.secureTextEntry = YES;
cell.accessoryView = password;
password = textField;
}
// ... rest of your work
return cell;
}
This is a issue because of the cell reusing. you need to change your cell a bit: add a textLabel in the if(cell==nil) branch, but configure it as username or password outside of the branch.
Firstly, the section:
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if( cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Login Ident"];</code>
should be
static NSString *identifier = #"Login Ident";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:identifier];
if( cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];</code>
Secondly, creating your text fields should be done outside the if (cell == nil) section.
it' possible to change position oh image and lable of my tableview's cell?
I would not use CustomCell..
I have tried this (for the label)..
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
return cell;
UIImage *image = nil;
cell.textLabel.text = #"text label";
UILabel *label = cell.textLabel;
CGRect frame = label.frame;
frame.origin.x = 62;
frame.origin.y = 62;
label.frame = frame;
image = [UIImage imageNamed:#"icon.png"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.imageView.image = image;
return cell;
}
Please take one UIview..and give frame size same as cell size...after that add label in that view...also whatever u want to add..add as a subview in that view...than add that view in cell .
All The Best
//table cell contains title,date,image
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell =nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
//tittle label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:contentFrame] ;
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:12];
titleLabel.text = [currentTweet title];
[cell.contentView addSubview:titleLabel];
//date label
UILabel *dateLabel = [[UILabel alloc] initWithFrame:dateFrame];
dateLabel.font = [UIFont systemFontOfSize:10];
dateLabel.text = [currentTweet dateCreated];
[cell.contentView addSubview:dateLabel];
//image view
CGRect imageFrame = CGRectMake(2, 8, 40,50);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
customImage.image =image;
[cell.contentView addSubview:self.customImage];
return cell;
After loading a tableview with custom cells all looks fine and scrolling down is fine.
When I scroll up the cells above appear blank. They are in fact fully functional as they can still be selected and then they go off to the details pages.
The code cellForRowAtIndexPath is executed for the returning row and returns the cell as I would expect with the right details. It just isn't displayed.
Any thoughts/help would be appreciated.
The code below is what I have been using.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[CustomCell class]]) {
cell = (CustomCell *)oneObject;
}
}
}
// Configure the cell.
Book *aBook = [appDelegate.sortedArray objectAtIndex:indexPath.row];
//name
NSString *trimmedString = [aBook.Name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.nameLabel.text = trimmedString;
//catagory
NSString *trimmedExperience = [aBook.PrimaryExperience stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimmedExperience isEqualToString:#"1"]) {
cell.catagoryLabel.text = #"None";
}
else if([trimmedExperience isEqualToString:#"2"]){
cell.catagoryLabel.text = #"Limited";
}
else if ([trimmedExperience isEqualToString:#"4"]) {
cell.catagoryLabel.text = #"Full";
}
else {
cell.catagoryLabel.text = #"";
}
cell.distanceLabel.text = [NSString stringWithFormat:#"%1.1f",aBook.distance];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
The problem lies with the xib file and how it loaded. To cut out the issue and gain total control the following code was substituted for the IB version of the custom cell.
static NSString *CellTableIdentifier = #"CellTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
CGRect nameLabelRect = CGRectMake(15, 5, 200, 15);
UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = UITextAlignmentLeft;
nameLabel.font = [UIFont boldSystemFontOfSize:14];
nameLabel.tag = kNameTag;
[cell.contentView addSubview:nameLabel];
[nameLabel release];
CGRect catagoryLabelRect = CGRectMake(15, 26, 100, 15);
UILabel *catagoryLabel = [[UILabel alloc]initWithFrame:catagoryLabelRect];
catagoryLabel.textAlignment = UITextAlignmentLeft;
catagoryLabel.font = [UIFont systemFontOfSize:12];
catagoryLabel.tag = kExperienceTag;
[cell.contentView addSubview:catagoryLabel];
[catagoryLabel release];
CGRect distanceLabelRect = CGRectMake(210, 26, 70, 15);
UILabel *distanceLabel = [[UILabel alloc] initWithFrame:distanceLabelRect];
distanceLabel.textAlignment = UITextAlignmentRight;
distanceLabel.font = [UIFont boldSystemFontOfSize:12];
distanceLabel.tag = kDistanceTag;
[cell.contentView addSubview:distanceLabel];
[distanceLabel release];
}
Thanks for helping think this one through. Now the scrolling works perfectly.