I have a UITableView that is grouped into sections and has a number of rows in each section, all the rows are disabled so they only show information and they do not allow user interaction except for the very last row. But when the user scrolls through the table the last row is getting copied to some of the other rows in the table. Here is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
UIFont *textLabelFont = [ UIFont fontWithName: #"Arial" size: 13.0 ];
UIFont *detailLabelFont = [UIFont fontWithName:#"Arial" size:13.0];
cell.textLabel.font = textLabelFont;
cell.detailTextLabel.font = detailLabelFont;
// Configure the cell.
[populatePolicyCells ProcessPolicyCell:cell IndexPath:indexPath];
if (indexPath.section == 3){
tableView.allowsSelection = YES;
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.userInteractionEnabled = YES;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.text = #"Manage Policy";
}
return cell;
}
Can anyone help?
Set up a different CellIdentifier for your "special cell"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell* cell = nil;
if (indexPath.section == 3)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"SpecialCell"];
if(cell == nil)
{
tableView.allowsSelection = YES;
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:"SpecialCell"] autorelease];
cell.userInteractionEnabled = YES;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
cell.textLabel.text = #"Manage Policy";
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
UIFont *textLabelFont = [ UIFont fontWithName: #"Arial" size: 13.0 ];
UIFont *detailLabelFont = [UIFont fontWithName:#"Arial" size:13.0];
cell.textLabel.font = textLabelFont;
cell.detailTextLabel.font = detailLabelFont;
}
// Configure the cell.
[populatePolicyCells ProcessPolicyCell:cell IndexPath:indexPath];
}
return cell;
}
change the dequeueCellWithIdentifier , everytime you reuse the tableview
Related
-(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];
}
// NSLog(#"msgcnt123 %#\n",[messageCount objectAtIndex:indexPath.row]);
NSArray *seperateArray = [[clist objectForKey:[[clist allKeys]objectAtIndex:indexPath.row]]componentsSeparatedByString:#"#"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// NSLog(#"key %#\n",[[clist allKeys]objectAtIndex:indexPath.row]);
cell.textLabel.text = [seperateArray objectAtIndex:0];
// cell.textLabel.text = [contactlist objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// NSLog(#"sep %#\n",seperateArray);
if (![[seperateArray objectAtIndex:1] isEqualToString:#"0"]) {
NSLog(#"msgCount %#\n",[seperateArray objectAtIndex:1]);
lblCnt = [[UILabel alloc]initWithFrame:CGRectMake(260, 13, 20, 20)];
lblCnt.backgroundColor = [UIColor lightGrayColor];
lblCnt.textColor = [UIColor blackColor];
lblCnt.text = [seperateArray objectAtIndex:1];
lblCnt.textAlignment = UITextAlignmentCenter;
lblCnt.layer.masksToBounds = YES;
lblCnt.layer.cornerRadius = 2.0f;
[cell.contentView addSubview:lblCnt];
}
else
{
NSLog(#"msgCount1 %#\n",[seperateArray objectAtIndex:1]);
[lblCnt removeFromSuperview];
lblCnt.hidden = YES;
}
return cell;
}
I have added a label in each row which displays number of messages received.In didSelect method i make label count zero so i can disappear label from tableView.In case of more than one row in table view label not disappear.
The simple and dirty method to reach what u want is to use a tag
-(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];
}
NSArray *seperateArray = [[clist objectForKey:[[clist allKeys]objectAtIndex:indexPath.row]]componentsSeparatedByString:#"#"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [seperateArray objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (![[seperateArray objectAtIndex:1] isEqualToString:#"0"]) {
lblCnt = [[UILabel alloc]initWithFrame:CGRectMake(260, 13, 20, 20)];
lblCnt.backgroundColor = [UIColor lightGrayColor];
lblCnt.textColor = [UIColor blackColor];
lblCnt.text = [seperateArray objectAtIndex:1];
lblCnt.textAlignment = UITextAlignmentCenter;
lblCnt.layer.masksToBounds = YES;
lblCnt.layer.cornerRadius = 2.0f;
[cell.contentView addSubview:lblCnt];
//Add a tag
lblCnt.tag = 1000;
}
else
{
/*
NSLog(#"msgCount1 %#\n",[seperateArray objectAtIndex:1]);
[lblCnt removeFromSuperview];
lblCnt.hidden = YES;
*/
for (UIView *view in [cell.contentView subviews]) {
if (view.tag == 1000) {
[view removeFromSuperview];
}
}
}
return cell;
}
and select the view based on the tag.
I want add label to every row in tableview at right side of the row in iphone programmatically.
Can anyone help me?
-(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] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
lbl.font = [UIFont boldSystemFontOfSize:17.0];
lbl.text = [self.arrRows objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl];
[lbl release];
return cell;
You can achieve it like this :
- (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] autorelease];
}
UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(200, 3, 100, 15)]; // For right alignment
lblText.text = [self.arrText objectAtIndex:indexPath.row];
lblText.textColor = [UIColor blackColor];
[cell addSubview:lblText];
[lblText release];
return cell;
}
I think you are asking for code.
try
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *yourLabel = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
yourLabel = [[UILabel alloc] initWithFrame:CGRect……];
[yourLabel setTag:9999];
[[cell contentView] addSubview:addressLabel];
}
if (!yourLabel)
yourLabel = (UILabel*)[cell viewWithTag:9999];
return cell;
}
In your cellForRowAtIndexPath: method :-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//set reuseIdentifier:nil for avoid label overlapping
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
//make your label as according to you
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
label.backgroundColor = [UIColor redColor];
// add label content view of cell
[cell.contentView addSubview:label];
return cell;
}
I have a settings view with 3 sections. Some cells have different styles: Default or Value1. When I swipe fast up or down, or change view and come back, the text supposed to be in a cell (for example the detailTextLabel in my cell with StyleValue1) is either not here anymore, or sometimes in a cell above or below... Here is the screenshots: the first is the normal state, the second the detailTextLabel from Version went to the cell above, and in the third the Measurement System detailTextLabel disappeared...
And here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (indexPath.section == 1 && indexPath.row == 0) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else if (indexPath.section == 2 && indexPath.row == 2) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
}
// Selection style.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// Vehicles cells.
if (indexPath.section == 0 && indexPath.row < [self.userCarsArray count]) {
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.text = [[NSString stringWithFormat:#"%# %# %#",
[[self.userCarsArray objectAtIndex:indexPath.row] year],
[[self.userCarsArray objectAtIndex:indexPath.row] make],
[[self.userCarsArray objectAtIndex:indexPath.row] model]] uppercaseString];
// Checkmark if current car.
if ([[EcoAppAppDelegate userCar] idCar] == [[self.userCarsArray objectAtIndex:indexPath.row] idCar]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
selectedCarPath = indexPath;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
// Add car cell.
if (indexPath.section == 0 && indexPath.row == [self.userCarsArray count]) {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.text = #"Add Vehicle";
}
// General cells.
if (indexPath.section == 1 && indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Measurement System";
cell.textLabel.textColor = [UIColor darkGrayColor];
if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)
cell.detailTextLabel.text = #"Miles";
else
cell.detailTextLabel.text = #"Meters";
}
// Information cells.
if (indexPath.section == 2 && indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"About";
cell.textLabel.textColor = [UIColor darkGrayColor];
}
if (indexPath.section == 2 && indexPath.row == 1) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"License";
cell.textLabel.textColor = [UIColor darkGrayColor];
}
if (indexPath.section == 2 && indexPath.row == 2) {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = #"Version";
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
Do you know how I can fix this issue? Thanks!
All your cells use the same reuseIdentifier, so when you scroll, you get an old cell and you set texts on it
You can solve your problem by setting
cell.detailTextLabel.text
for all cases
When using reuseIdentifier, you should set every time all fields content that change
- (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] autorelease];
}
// Configure the cell...
if(indexPath.row < 8)
{
CGRect textRect = CGRectMake(10, 10, 300, 31);
UITextField *myfield = [[UITextField alloc]initWithFrame:textRect];
myfield.borderStyle = UITextBorderStyleRoundedRect;
myfield.font = [UIFont systemFontOfSize:22.0];
myfield.adjustsFontSizeToFitWidth = YES;
myfield.minimumFontSize = 2.0;
myfield.clearButtonMode = UITextFieldViewModeWhileEditing;
myfield.keyboardType = UIKeyboardTypeDefault;
myfield.autocorrectionType= UITextAutocorrectionTypeNo;
myfield.autocapitalizationType=UITextAutocapitalizationTypeNone;
myfield.returnKeyType=UIReturnKeyDone;
[self. addSubview:myfield];
}
return cell;
}
I wrote the above code for displaying textboxes in UITableViewCell (up to eight cells ) but the text box is displaying in the first cell only is there anything wrong in the above code?
The problem is this line:
[self.addSubview:myfield];
Change this to:
[cell.contentView addSubview: myfield];
[myfield release];
However, I agree with Björn Marschollek about design, so here is how this method should look:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierWithTextBox = #"CellWithTextBox";
static NSString *CellIdentifierOther = #"CellOther";
UITableViewCell *cell;
if(indexPath.row < 8)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierWithTextBox];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierWithTextBox] autorelease];
CGRect textRect = CGRectMake(10, 10, 300, 31);
UITextField *myfield = [[UITextField alloc]initWithFrame:textRect];
myfield.borderStyle = UITextBorderStyleRoundedRect;
myfield.font = [UIFont systemFontOfSize:22.0];
myfield.adjustsFontSizeToFitWidth = YES;
myfield.minimumFontSize = 2.0;
myfield.clearButtonMode = UITextFieldViewModeWhileEditing;
myfield.keyboardType = UIKeyboardTypeDefault;
myfield.autocorrectionType= UITextAutocorrectionTypeNo;
myfield.autocapitalizationType=UITextAutocapitalizationTypeNone;
myfield.returnKeyType=UIReturnKeyDone;
[cell.contentView addSubview: myfield];
[myfield release];
}
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierOther];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierOther] autorelease];
}
}
return cell;
}
I have a UITableView that can be edited. I am displaying cells as such:
- (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] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...
STWatchList *mySTWatchList;
mySTWatchList = [items objectAtIndex:indexPath.row];
cell.textLabel.text = mySTWatchList.watchListName;
return cell;
}
When the user is editing, I would like to show the disclosure indicator. How can I accomplish this?
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (indexPath.row==0)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}