I am adding an activity indicator in each row of table. The issue is every time I scroll it get added again in cell overwriting the previous one. Please let me know which is best way to add control in tableview cell.
UIActivityIndicatorView *a;
// Customize the appearance of table view cells.
-(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];
}
a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[a startAnimating];
[cell.contentView addSubview:a];
// Configure the cell.
return cell;
}
Add indicator to a cell only when you create it, then you can access it using its tag property:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
a.tag = 10;
[cell.contentView addSubview:a];
}
UIActivityIndicatorView* aView = [cell.contentView viewWithTag:10];
[aView startAnimating];
Related
I am having more than a table view in a view controller, it is giving error. I have no idea what is where going wrong can anyone guide. Data is not showing in table view but array has data when printed in console, i tried without array also providng static data to cell.textLabel.text, but not showing in tableview, numbers of rows checked no issue there. control does not go to cell.textLabel.text, i am not getting what is wrong. Please guide for the above. Below is the code:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
if(tableView == self.mLocationTable)
{
cell = [self.mLocationTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// if([self.mLocShowArr count]!= 0)
// {
// NSString *str = [self.mLocShowArr objectAtIndex:indexPath.row];
cell.textLabel.text = #"locations";
// NSLog(#"show loc:%#", [self.mLocShowArr objectAtIndex:0]);
// }
}
if(tableView == self.mNotifyTable)
{
cell = [self.mNotifyTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImage *yourImage = [UIImage imageNamed:#"emma-watson-02.jpg"];
UIImageView *imageViewToPutInCell = [[UIImageView alloc] initWithImage:yourImage];
imageViewToPutInCell.frame = CGRectMake(5, 8, 55, 55);
UILabel *cellTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(68, 10, 200, 40)];
cellTextLabel.text = #"notification view";
[cell addSubview:imageViewToPutInCell];
[cell addSubview:cellTextLabel];
}
}
This is the complete error : Error [IRForTarget]: Call to a symbol-only function 'objc_msgSend' that is not present in the target.
Things to check.
Data source methods all required implemented
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
log the return value and make sure it is not 0
Give the different cell identifier for different tables
Make sure table data source and delegates are connected in the nib
Error Explanation here
Try this code !!! In your code you missed "return cell;"
-(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 = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryNone;
}
if(tableView == self.mNotifyTable) {
cell.textLabel.text = #"locations";
UIImage *yourImage = [UIImage imageNamed:#"emma-watson-02.jpg"];
UIImageView *imageViewToPutInCell = [[UIImageView alloc] initWithImage:yourImage];
imageViewToPutInCell.frame = CGRectMake(5, 8, 55, 55);
UILabel *cellTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(68, 10, 200, 40)];
cellTextLabel.text = #"notification view";
[cell addSubview:imageViewToPutInCell];
[cell addSubview:cellTextLabel];
}
return cell;
}
Hope this solves your problem !!!
Accept the answer if it helps you !!!
I am using a grouped table view for my iPhone application, for which I need to change the selected background color to red. I can set it, but the problem is that the first table cell the views are outside the table cell.
try to 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];
}
cell.backgroundColor = [UIColor clearColor]; // clear the cell background color
// Configure the cell.
return cell;
}
You can use following code
1)cell.selectionStyle = UITableViewCellSelectionStyleRed;
OR
2)cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
cell.selectedBackgroundView = selectionColor;
use any of above code will solve your problem if still your problem is not solved put your code i'll try to solve
I've got a tableview loaded from an array and for some reason it's re-using the cell but loading the same information lower down the table, a second time.
http://screencast.com/t/Ig8bcqSpLzp
The video above should give you an idea of what I mean.
This is my code to load the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
accessicon *current =[arryAccess objectAtIndex:indexPath.row] ;
cell.textLabel.text = current.text;
cell.imageView.image = [UIImage imageNamed: current.iconPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
switchView.myValue = current.iconValue;
if(totalIconValue & current.iconValue) {
[switchView setOn: YES animated:NO];
} else {
[switchView setOn: NO animated:NO];
}
[switchView addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
return cell;
}
Any help will be appreciated
EDIT: I've updated my code above, however on the actual device it's still selecting items it shouldn't be - no longer reusing the names but if i scroll up and down it selects them on it's own?
see the video here: http://screencast.com/t/a9N1qbws
Tom
This code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
if (cell == nil) {
...
is fundamentally wrong. It should read:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
...
And then, anything that is row-specific should be outside that if statement.
The pattern for this, generally, should be:
dequeue a reusable cell
if you didn't get one:
allocate a new cell
perform generic cell setup, i.e. setup that applies to all cells
setup the cell for the specific row, and return it.
Hope this helps.
You aren't reusing you cells. And you are filling the cells only when the init fails. Try this code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
accessicon *current =[arryAccess objectAtIndex:indexPath.row] ;
cell.textLabel.text = current.text;
cell.imageView.image = [UIImage imageNamed: current.iconPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
switchView.myValue = current.iconValue;
if(totalIconValue & current.iconValue) {
[switchView setOn: YES animated:YES];
} else {
[switchView setOn: NO animated:YES];
}
[switchView addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
Or develop under ios5 and get rid of the alloc and init.
You are constantly creating new rows instead of reusing the old ones.
Move the line
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
inside the if block, end move everything else you have in the if block outside of it, except the lines
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
You need to create the switch only once for row, when it is initialized (that means, in the if statement). I suggest you to create it and assign it a tag, so you can access it later by its tag. Your code now is creating a new switch everytime the cell is used, this could be the reason why the switches are behaving this way.
I'm trying to customize how my UITableView looks by setting the backgroundView with an UIImageView, but is not working properly as you can see in the image. (only works for the accessoryType and at the start of the row before the text.)
Can anybody help me with this?
My code:
// Customize the appearance of table view cells.
- (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.
NSDictionary *lineaAutobus = [buscamionService objectAtIndex:indexPath.row];
NSString *lineaAutobusNombre = [lineaAutobus objectForKey:#"nombre_lineaAutobus"];
[cell setBackgroundView:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tableViewCellBg.png"]] autorelease]];
cell.textLabel.text = lineaAutobusNombre;
return cell;
}
https://www.dropbox.com/s/ubok76ipshei931/Screen%20shot%202011-05-20%20at%206.25.29%20PM.png
Perhaps you need to set the backgroundColor of the textLabel to [UIColor clearColor]?
i am displaying array of data in a table view.
my code for that is like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
CGRect labelFrame = CGRectMake(0,0 , 100, 25);
itemNameLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
itemNameLabel.textAlignment = UITextAlignmentLeft;
itemNameLabel.backgroundColor = [UIColor clearColor];
itemNameLabel.text = [tableData objectAtIndex:indexPath.row];
CGRect anotherLabelFrame = CGRectMake(120, 0, 100, 25);
quantityLabel = [[[UILabel alloc] initWithFrame:anotherLabelFrame] autorelease];
quantityLabel.textAlignment = UITextAlignmentLeft;
quantityLabel.backgroundColor = [UIColor clearColor];
MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
Category *obj = (Category *)[appDelegate.itemsList objectAtIndex:indexPath.row];
quantityLabel.text = [NSString stringWithFormat:#"%d",obj.quantity];
[cell.contentView addSubview:itemNameLabel];
[cell.contentView addSubview:quantityLabel];
return cell;
}
how can i resolve this.
can any one please help me.
Thank u in advance.
i am giving text for itemNameLabel is using array and for quantityLabel using class object.
so,when i scrolling table view the array names are override on other label,but quantity label is not overriding because it is from class.
i need to display itemNameLabel from array since it changes it value.
when i scroll table view my tableview is looking like this.
Ideally you should alloc all lable in this section
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
and in the else part you should just Tag those labels and reuse after it but there is another short and sweet way
if Application is not much resource consuming
change
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
Try setting Height for each row using the function
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0f;
}