i have a performance issue in my tableview
my cellForRow looks like that:
if (tableView == allMonthTable) {
static NSString *cellIdentifier = #"cell";
AllMonthCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[AllMonthCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] ;
}
Month *mo = [[dataHandler allMonth]objectAtIndex:indexPath.section];
double budgetValue = mo.budget.doubleValue;
[cell.budgetLabel setText:[NSString stringWithFormat:#"%.2f",budgetValue ]];
double spentValue = mo.spent.doubleValue;
[cell.spentLabel setText:[NSString stringWithFormat:#"%.2f",spentValue ]];
if (spentValue > 0) {
[cell.spentLabel setText:[NSString stringWithFormat:#"+%.2f",spentValue]];
[cell.spentLabel setTextColor:[self greenColor]];
}
else if (spentValue < 0){
[cell.spentLabel setText:[NSString stringWithFormat:#"%.2f",spentValue]];
[cell.spentLabel setTextColor:[self redColor]];
}
double balanceValue = budgetValue+spentValue;
[cell.balanceLabel setText:[NSString stringWithFormat:#"%.2f",balanceValue ]];
if (balanceValue > 0) {
[cell.balanceLabel setText:[NSString stringWithFormat:#"+%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self greenColor]];
}
else{
[cell.balanceLabel setText:[NSString stringWithFormat:#"%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self redColor]];
}
double avgDayValue = mo.avgDay.doubleValue;
[cell.avgDayLabel setText:[NSString stringWithFormat:#"%.2f",avgDayValue ]];
if (avgDayValue > 0) {
[cell.avgDayLabel setText:[NSString stringWithFormat:#"+%.2f",avgDayValue]];
}
int numberOfExpValue = mo.expenses.intValue;
[cell.numberOfExpLabel setText:[NSString stringWithFormat:#"%d",numberOfExpValue ]];
return cell;
}
return nil;
}
and my AllMonthCell.m files looks like that:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIFont *font = [UIFont fontWithName:#"Cochin" size:14.0];
UIFont *fontBold = [UIFont fontWithName:#"Cochin-Bold" size:14.0];
self.frame = CGRectMake(0, 0, 312, 100);
UIView *top = [[UIView alloc]initWithFrame:CGRectMake(4, 0, 304, 1)];
[top setBackgroundColor:[UIColor lightGrayColor]];
UILabel *budgetStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 102, 16)];
[budgetStringLabel setTextAlignment:UITextAlignmentLeft];
[budgetStringLabel setFont:font];
[budgetStringLabel setTextColor:[UIColor lightGrayColor]];
[budgetStringLabel setText:#"Month Budget:"];
[budgetStringLabel setBackgroundColor:[self grayColor]];
UILabel *spentStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 102, 16)];
[spentStringLabel setTextAlignment:UITextAlignmentLeft];
[spentStringLabel setFont:font];
[spentStringLabel setTextColor:[UIColor lightGrayColor]];
[spentStringLabel setText:#"Spent Money:"];
[spentStringLabel setBackgroundColor:[self grayColor]];
UIView *divide1 = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 292, 1)];
[divide1 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *balanceStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, 102, 16)];
[balanceStringLabel setTextAlignment:UITextAlignmentLeft];
[balanceStringLabel setFont:font];
[balanceStringLabel setTextColor:[UIColor lightGrayColor]];
[balanceStringLabel setText:#"Month Balance:"];
[balanceStringLabel setBackgroundColor:[self grayColor]];
UIView *divide2 = [[UIView alloc]initWithFrame:CGRectMake(10, 74, 292, 1)];
UIView *divide3 = [[UIView alloc]initWithFrame:CGRectMake(10, 76, 292, 1)];
[divide2 setBackgroundColor:[UIColor lightGrayColor]];
[divide3 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *avgDayStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 94, 200, 16)];
[avgDayStringLabel setTextAlignment:UITextAlignmentLeft];
[avgDayStringLabel setFont:font];
[avgDayStringLabel setTextColor:[UIColor lightGrayColor]];
[avgDayStringLabel setText:#"Per Day:"];
[avgDayStringLabel setBackgroundColor:[self grayColor]];
UILabel *numberOfExpStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 114, 102, 16)];
[numberOfExpStringLabel setTextAlignment:UITextAlignmentLeft];
[numberOfExpStringLabel setFont:font];
[numberOfExpStringLabel setTextColor:[UIColor lightGrayColor]];
[numberOfExpStringLabel setText:#"Expenses Made:"];
[numberOfExpStringLabel setBackgroundColor:[self grayColor]];
[self addSubview:budgetStringLabel];
[self addSubview:top];
[self addSubview:spentStringLabel];
[self addSubview:divide1];
[self addSubview:balanceStringLabel];
[self addSubview:divide2];
[self addSubview:divide3];
[self addSubview:numberOfExpStringLabel];
[self addSubview:avgDayStringLabel];
self.budgetLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 102, 16)];
[self.budgetLabel setTextAlignment:UITextAlignmentRight];
[self.budgetLabel setFont:font];
[self.budgetLabel setBackgroundColor:[self grayColor]];
[self.budgetLabel setTextColor:[UIColor darkGrayColor]];
self.spentLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 30, 102, 16)];
[self.spentLabel setTextAlignment:UITextAlignmentRight];
[self.spentLabel setFont:font];
[self.spentLabel setBackgroundColor:[self grayColor]];
[self.spentLabel setTextColor:[UIColor lightGrayColor]];
self.balanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 55, 102, 16)];
[self.balanceLabel setTextAlignment:UITextAlignmentRight];
[self.balanceLabel setFont:fontBold];
[self.balanceLabel setBackgroundColor:[self grayColor]];
self.avgDayLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 94, 102, 16)];
[self.avgDayLabel setFont:font];
[self.avgDayLabel setBackgroundColor:[self grayColor]];
[self.avgDayLabel setTextColor:[UIColor lightGrayColor]];
[self.avgDayLabel setTextAlignment:UITextAlignmentRight];
self.numberOfExpLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 114, 102, 16)];
[self.numberOfExpLabel setTextAlignment:UITextAlignmentRight];
[self.numberOfExpLabel setFont:font];
[self.numberOfExpLabel setBackgroundColor:[self grayColor]];
[self.numberOfExpLabel setTextColor:[UIColor lightGrayColor]];
[self addSubview:self.budgetLabel];
[self addSubview:self.spentLabel];
[self addSubview:self.balanceLabel];
[self addSubview:self.numberOfExpLabel];
[self addSubview:self.avgDayLabel];
[self setBackgroundColor:[self grayColor]];
}
return self;
}
here is a screenshot of time profiling while scrolling:
so i create reusable cells of the type AllMonthCell, and assign the values to them, is there a way to make it faster? i have pretty serious lags while scrolling the table..
a hint would be great :)
You should reuse the cells
Let me derive you a pattern
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UILabel *noExpense;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
//Just the view alone should be created here no assigning of data here
//let me add just one custom view for easy understanding
noExpense = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 296, 16)];
[noExpense setTextAlignment:UITextAlignmentCenter];
[noExpense setFont:fontName];
//[noExpense setText:#"No data available yet."]; Dont do this
[noExpense setTextColor:[UIColor lightGrayColor]];
[noExpense setBackgroundColor:tableView.backgroundColor];
[noExpense setTag:100]; //Must set tag here
[cell addSubview:noExpense];
}
else
{
noExpense = [cell viewWithTag:100];
//You have to initialize your custom views which you created already.
//Just the view alone should be assigned here no assigning of data here
}
//Load all the data into the views here
[noExpense setText:#"Somedata which you have to set"];
return cell;
}
To know more about tableview see the doc. Its the bible for understanding tableviews.
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7
Related
Hello fellow programmers,
I am really cracking my head trying to understand what the problem is, but I just can't seem to figure out the problem. I have done research on this but nothing works for me unfortunately. The problem is that I have multiple cells and each cell is given a specific UITextField which I draw on the cellForRowAtIndexPath programmatically. The issues is that when I enter some information on that any Text Field and I scroll down on the table view, the text on the text field disappears into thin air. I am trying to understand why the cell reuse (which I believe is causing the problem), is causing this.
Here is my code for the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
cellText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 260, 20)];
cellText.font = [UIFont boldSystemFontOfSize:11.0];
cellText.textColor = [UIColor grayColor];
[cell.contentView addSubview:cellText];
if (indexPath.section == 0)
{
cellText.text = clientInfoArray[indexPath.row];
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Customer Name"])
{
nameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
nameTxtField.placeholder = #"full name";
nameTxtField.font = [UIFont boldSystemFontOfSize:15.0];
nameTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[nameTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:nameTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Phone"])
{
phoneTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
phoneTxtField.placeholder = #"xxx-xxx-xxx";
phoneTxtField.font = [UIFont boldSystemFontOfSize:15.0];
phoneTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
phoneTxtField.keyboardType = UIKeyboardTypePhonePad;
[phoneTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:phoneTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Date"])
{
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
dateLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:dateLabel];
dateLabel.text = [dateFormatter stringFromDate:[NSDate date]];
}
}
if (indexPath.section == 1) {
cellText.text = vehicleInfoArray[indexPath.row];
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Make and Model"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
modelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:modelLabel];
modelLabel.text = makeAndModelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Color"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
colorLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:colorLabel];
colorLabel.text = colorData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Doors"])
{
doorsTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
doorsTxtField.placeholder = #"#";
doorsTxtField.font = [UIFont boldSystemFontOfSize:15.0];
doorsTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[doorsTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
doorsTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:doorsTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Vehicle VIN"])
{
vinTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
vinTxtField.placeholder = #"#";
vinTxtField.font = [UIFont boldSystemFontOfSize:15.0];
vinTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[vinTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
vinTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:vinTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Fuel"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
fuelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
fuelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:fuelLabel];
fuelLabel.text = fuelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Milage"])
{
milageLabel = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
milageLabel.placeholder = #"ODO";
milageLabel.font = [UIFont boldSystemFontOfSize:15.0];
milageLabel.autocorrectionType = UITextAutocorrectionTypeNo;
[milageLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
milageLabel.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:milageLabel];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Other description"])
{
otherDTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, 300, 125)];
otherDTxtView.font = [UIFont boldSystemFontOfSize:15.0];
[cell addSubview:otherDTxtView];
}
}
if (indexPath.section == 2) {
cellText.text = InAndOutInfoArray[indexPath.row];
}
return cell;
}
Any ideas on how I can fix this problem? + Extra points for a helpful answer. :)
Whenever you are using reuse cell techniques, the cell that is not visible will be deallocated to free some memory.
Since you are creating a textfield in the cellForRow method, it will also be deallocated when the cell is not visible and redrawn when it becomes visible.
You could save the textfield value in some array (using textfield's didEndEditing delegate) and use that array to populate the text during textfield creation process inside the cellForRow method.
I have a three tab UITabBar in which the background of each tab is set, along with the background of the tabbar itself, as seen in the first image below. Each UITabBarItem has its FinishedSelectedImage and FinishedUnselectedImage set, and an NSString to its title. These images are glyphs (icons).
However, having set the selectionIndicatorImage to an image sized 106x48px, there is ~1px space to the right of the third tab, when in its selected state (see second photo).
Please can you tell me how I can remove the additional space? I have tried using an image sized 1px wider, but that made no difference.
Edit--- I read that this could be a problem due to an incorrectly sized selectionIndicatorImage, but I can confirm that this was not a fix -- I tried using a 107x49px image, and that too did not make a difference.
Max.
Tab Bar (how it should be upon selection)
Tab Bar (how it shows up on the third tab - notice the 1px on the right)
// customise the UITabBar - set the background image and the selected background image
[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tab_bar"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selection_bg_3"]];
// this is in a for loop.
UITabBarItem *item = [[UITabBarItem alloc] init];
[item setTitle:[titles objectAtIndex:index]];
[item setImage:[UIImage imageNamed:[image_titles objectAtIndex:index]]];
[item setFinishedSelectedImage:[UIImage imageNamed:[[image_titles objectAtIndex:index] stringByAppendingString:#"_selected"]] withFinishedUnselectedImage:[UIImage imageNamed:[image_titles objectAtIndex:index]]];
[navigationController setTabBarItem:item];
[item release];
I ended up subclassing UITabBarController using three UIImageViews as each tab's selectionIndicatorBackground, and then using a further UIImageView to display the tab's glyph, and a UILabel for displaying its title.
Here is the code:
#interface LPTabBarController : UITabBarController
#property (nonatomic, retain) UIImageView *firstTab, *secondTab, *thirdTab, *firstTabBG, *secondTabBG, *thirdTabBG;
#property (nonatomic, retain) UILabel *firstTabLabel, *secondTabLabel, *thirdTabLabel;
#end
#implementation LPTabBarController
#synthesize firstTab, secondTab, thirdTab, firstTabBG, secondTabBG, thirdTabBG, firstTabLabel, secondTabLabel, thirdTabLabel;
- (id) init {
if ((self = [super init])) {
[self initialise];
}
return self;
}
- (void) setViewControllers:(NSArray *)viewControllers {
[self setViewControllers:viewControllers animated:NO];
}
- (void) setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated {
[super setViewControllers:viewControllers animated:animated];
for (UITabBarItem *item in self.tabBar.items) {
[item setEnabled:NO];
}
}
- (void) initialise {
UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.tabBar.frame.origin.y - 10, 320, 59)];
[bg setUserInteractionEnabled:YES];
[bg setImage:[UIImage imageNamed:#"tab_bar"]];
CGFloat widthbg = ceilf(self.view.frame.size.width / 3);
CGFloat heightbg = self.tabBar.frame.size.height;
CGFloat width = 23, y = 5;
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
self.firstTab = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab_bar_user"]] autorelease];
[self.firstTab setFrame:CGRectMake(ceilf((widthbg - width) / 2), y, 23, 23)];
self.firstTabLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 27, widthbg, 20)] autorelease];
[self.firstTabLabel setTextAlignment:NSTextAlignmentCenter];
[self.firstTabLabel setText:#"Profile"];
[self.firstTabLabel setFont:[UIFont boldSystemFontOfSize:13]];
[self.firstTabLabel setTextColor:RGB(80, 23, 17)];
[self.firstTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]];
[self.firstTabLabel setShadowOffset:CGSizeMake(0, 1)];
[self.firstTabLabel setBackgroundColor:[UIColor clearColor]];
self.firstTabBG = [[[UIImageView alloc] initWithImage:nil] autorelease];
[self.firstTabBG setTag:01];
[self.firstTabBG setFrame:CGRectMake(0, 10, widthbg, heightbg)];
[self.firstTabBG addSubview:self.firstTab];
[self.firstTabBG addSubview:self.firstTabLabel];
[self.firstTabBG setUserInteractionEnabled:YES];
[self.firstTabBG addGestureRecognizer:gest];
[bg addSubview:self.firstTabBG];
[gest release];
gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
self.secondTab = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab_bar_explore_selected"]] autorelease];
[self.secondTab setFrame:CGRectMake(ceilf((widthbg - width) / 2), y, 23, 23)];
self.secondTabLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 27, widthbg, 20)] autorelease];
[self.secondTabLabel setTextAlignment:NSTextAlignmentCenter];
[self.secondTabLabel setText:#"Explore"];
[self.secondTabLabel setFont:[UIFont boldSystemFontOfSize:13]];
[self.secondTabLabel setTextColor:RGB(80, 23, 17)];
[self.secondTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]];
[self.secondTabLabel setShadowOffset:CGSizeMake(0, 1)];
[self.secondTabLabel setBackgroundColor:[UIColor clearColor]];
self.secondTabBG = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab_bar_selected_bg"]] autorelease];
[self.secondTabBG setTag:02];
[self.secondTabBG setUserInteractionEnabled:YES];
[self.secondTabBG addGestureRecognizer:gest];
[self.secondTabBG setFrame:CGRectMake(widthbg, 10, widthbg, heightbg)];
[self.secondTabBG addSubview:self.secondTab];
[self.secondTabBG addSubview:self.secondTabLabel];
[bg addSubview:self.secondTabBG];
[self tapped:gest];
[gest release];
gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
self.thirdTab = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab_bar_settings"]] autorelease];
[self.thirdTab setFrame:CGRectMake(ceilf((widthbg - width) / 2), y, 23, 23)];
self.thirdTabLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 27, widthbg, 20)] autorelease];
[self.thirdTabLabel setTextAlignment:NSTextAlignmentCenter];
[self.thirdTabLabel setText:#"Settings"];
[self.thirdTabLabel setFont:[UIFont boldSystemFontOfSize:13]];
[self.thirdTabLabel setTextColor:RGB(80, 23, 17)];
[self.thirdTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]];
[self.thirdTabLabel setShadowOffset:CGSizeMake(0, 1)];
[self.thirdTabLabel setBackgroundColor:[UIColor clearColor]];
self.thirdTabBG = [[[UIImageView alloc] initWithImage:nil] autorelease];
[self.thirdTabBG setTag:03];
[self.thirdTabBG setFrame:CGRectMake(widthbg * 2, 10, widthbg, heightbg)];
[self.thirdTabBG addSubview:self.thirdTab];
[self.thirdTabBG addSubview:self.thirdTabLabel];
[self.thirdTabBG setUserInteractionEnabled:YES];
[self.thirdTabBG addGestureRecognizer:gest];
[bg addSubview:self.thirdTabBG];
[gest release];
[self.view addSubview:bg];
[bg release];
}
static int lastIndex = 2;
- (void) tapped:(UITapGestureRecognizer *) gest {
switch (lastIndex) {
case 1:
[self.firstTab setImage:[UIImage imageNamed:#"tab_bar_user"]];
[self.firstTabBG setImage:nil];
[self.firstTabLabel setTextColor:RGB(80, 23, 17)];
[self.firstTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]];
[self.firstTabLabel setShadowOffset:CGSizeMake(0, 1)];
break;
case 2:
[self.secondTab setImage:[UIImage imageNamed:#"tab_bar_explore"]];
[self.secondTabBG setImage:nil];
[self.secondTabLabel setTextColor:RGB(80, 23, 17)];
[self.secondTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]];
[self.secondTabLabel setShadowOffset:CGSizeMake(0, 1)];
break;
case 3:
[self.thirdTab setImage:[UIImage imageNamed:#"tab_bar_settings"]];
[self.thirdTabBG setImage:nil];
[self.thirdTabLabel setTextColor:RGB(80, 23, 17)];
[self.thirdTabLabel setShadowColor:[UIColor colorWithWhite:1 alpha:0.35]];
[self.thirdTabLabel setShadowOffset:CGSizeMake(0, 1)];
break;
}
switch (gest.view.tag) {
case 1 :{
[self setSelectedIndex:0];
[self.firstTab setImage:[UIImage imageNamed:#"tab_bar_user_selected"]];
[self.firstTabBG setImage:[UIImage imageNamed:#"tab_bar_selected_bg"]];
[self.firstTabLabel setTextColor:RGB(252, 208, 170)];
[self.firstTabLabel setShadowColor:RGB(80, 23, 17)];
[self.firstTabLabel setShadowOffset:CGSizeMake(0, -1)];
break;
}
case 2: {
[self setSelectedIndex:1];
[self.secondTab setImage:[UIImage imageNamed:#"tab_bar_explore_selected"]];
[self.secondTabBG setImage:[UIImage imageNamed:#"tab_bar_selected_bg"]];
[self.secondTabLabel setTextColor:RGB(252, 208, 170)];
[self.secondTabLabel setShadowColor:RGB(80, 23, 17)];
[self.secondTabLabel setShadowOffset:CGSizeMake(0, -1)];
break;
}
case 3: {
[self setSelectedIndex:2];
[self.thirdTab setImage:[UIImage imageNamed:#"tab_bar_settings_selected"]];
[self.thirdTabBG setImage:[UIImage imageNamed:#"tab_bar_selected_bg"]];
[self.thirdTabLabel setTextColor:RGB(252, 208, 170)];
[self.thirdTabLabel setShadowColor:RGB(80, 23, 17)];
[self.thirdTabLabel setShadowOffset:CGSizeMake(0, -1)];
break;
}
}
lastIndex = gest.view.tag;
}
- (void) dealloc {
[firstTab release];
[firstTabBG release];
[secondTab release];
[secondTabBG release];
[thirdTab release];
[thirdTabBG release];
[firstTabLabel release];
[secondTabLabel release];
[thirdTabLabel release];
[super dealloc];
}
I can not get rid of this strange spacing between line 0 and 1.
I want the same spacing between lines 0 and 1 as I have between line 1 and 2.
[self.tableView setBackgroundColor:TABLE_VIEW_BACKGROUND_COLOR];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
UIImageView *headerBackgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"addres_box"]];
[headerBackgroundImageView setFrame:CGRectMake(headerView.frame.origin.x + 10, headerView.frame.origin.y + 10, headerView.frame.size.width - 20, 160)];
[headerView addSubview:headerBackgroundImageView];
UILabel *headerTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(headerBackgroundImageView.frame.origin.x + 10, headerBackgroundImageView.frame.origin.y + 10, headerBackgroundImageView.frame.size.width - 20, 20)];
[headerTextLabel setBackgroundColor:[UIColor clearColor]];
[headerTextLabel setFont:[UIFont boldSystemFontOfSize:headerTextLabel.font.pointSize]];
[headerTextLabel setTextColor:[UIColor whiteColor]];
[headerTextLabel setLineBreakMode:UILineBreakModeTailTruncation];
[headerTextLabel setNumberOfLines:0];
NSString *headerTextString = #"0";
NSString *detailTextString = #"1\n2";
[headerTextLabel setText:headerTextString];
[headerView addSubview:headerTextLabel];
_headerTextLabel = headerTextLabel;
UILabel *headerDetailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(headerTextLabel.frame.origin.x, headerTextLabel.frame.origin.y+20, headerTextLabel.frame.size.width, 140)];
[headerDetailTextLabel setBackgroundColor:[UIColor clearColor]];
[headerDetailTextLabel setTextColor:[UIColor whiteColor]];
[headerDetailTextLabel setLineBreakMode:UILineBreakModeTailTruncation];
[headerDetailTextLabel setNumberOfLines:0];
[headerDetailTextLabel setText:detailTextString];
[headerView addSubview:headerDetailTextLabel];
_headerDetailTextLabel = headerDetailTextLabel;
[self.tableView setTableHeaderView:headerView];
[self.tableView setSeparatorColor:TABLE_VIEW_SEPARATOR_COLOR];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
I hope someone can help me!
Perhaps your headerDetailTextLabel is centered vertically, try adding more lines like "1\n2\n3\n4" and see the result.
Thanks for the hints, I solved it by adding "sizeToFit" and now every things look good
[headerDetailTextLabel setText:detailTextString];
[headerDetailTextLabel sizeToFit]; //this line fixed the spacing problem
[headerView addSubview:headerDetailTextLabel];
I've got a standard UITableView where each cell is a custom UIView. Everything works fine, except when the cell is selected, it turns blue (as expected) but the UIView is hidden!
How can I change the contents of the view to be visible when the cell is selected?
- (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];
}
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 40)];
[headerLabel setText:[[[todayCollection events]objectAtIndex:indexPath.row]name]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:22]];
headerLabel.highlightedTextColor = [UIColor whiteColor];
UILabel *venueLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 300, 20)];
[venueLabel setText:[[[[todayCollection events]objectAtIndex:indexPath.row]venue]name]];
[venueLabel setFont:[UIFont italicSystemFontOfSize:16]];
LBVenueBadge *venueBadge = [[LBVenueBadge alloc] initWithGenre:[[[todayCollection events]objectAtIndex:indexPath.row]genre] frame:CGRectMake(85, 73, 100, 20)];
UIImageView *pinImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 75, 18, 18)];
[pinImage setImage:[UIImage imageNamed:#"193-location-arrow.png"]];
UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 75, 100, 15)];
[distanceLabel setFont:[UIFont systemFontOfSize:12]];
NSString *distanceString = [[[[todayCollection events]objectAtIndex:indexPath.row]venue]distanceString];
if([[[[todayCollection events]objectAtIndex:indexPath.row]performances]count] == 1)
{
UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
[performanceLabel setText:[[[[[todayCollection events]objectAtIndex:indexPath.row]performances]objectAtIndex:0]time]];
[performanceLabel setBackgroundColor:[UIColor clearColor]];
[performanceLabel setFont:[UIFont systemFontOfSize:12]];
[containerView addSubview:performanceLabel];
}else{
UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
[performanceLabel setText:[NSString stringWithFormat:#"%i shows", [[[[todayCollection events]objectAtIndex:indexPath.row]performances]count]]];
[performanceLabel setBackgroundColor:[UIColor clearColor]];
[performanceLabel setFont:[UIFont systemFontOfSize:12]];
[containerView addSubview:performanceLabel];
}
[distanceLabel setText:distanceString];
[containerView addSubview:pinImage];
[distanceLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:distanceLabel];
[headerLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:headerLabel];
[venueLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:venueLabel];
[containerView addSubview:venueBadge];
if(indexPath.row % 2 == 0)
{
[containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.05]];
}else{
[containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.02]];
}
cell.backgroundView = containerView;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
Thanks,
As the contentView property is readonly, I can add a subview to it instead.
[cell.contentView addSubview:containerView];
You are adding views to the background rather than the contentView of the cell replace this line.
cell.backgroundView = containerView;
with
[cell.contentView addSubview:containerView];
I have created a table view and added some labels and displaying it, now i need a button in each cell, where on click on the button, it has redirect to the new screen.
The following is the code which i am trying to redirect,
- (void)viewDidLoad
{
vendorsArray = [CommonUserDetails sharedUserDetails].mVendorDictionary;
NSLog(#"vendorsArray is %# \n count is %d",vendorsArray,[vendorsArray count]);
loTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 43, 320, 400) style:UITableViewStylePlain];
[loTableView setBackgroundColor:[UIColor whiteColor]];
loTableView.delegate = self;
loTableView.dataSource = self;
loTableView.separatorColor = [UIColor grayColor];
[self.view addSubview:loTableView];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifier:#"myCell"];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[self configureCell:cell forIndexPath:indexPath];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#define L1_TAG 1
#define L2_TAG 2
#define L3_TAG 3
#define L4_TAG 4
- (UITableViewCell *)tableviewCellWithReuseIdentifier:(NSString *)identifier
{
UITableViewCell* cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, 320, 100)reuseIdentifier:identifier];
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 290, 30)];
[l1 setBackgroundColor:[UIColor clearColor]];
[l1 setFont:[UIFont systemFontOfSize:14]];
// [l1 setTextColor:[UIColor blackColor]];
l1.textColor = [UIColor colorWithRed:153/(CGFloat)255 green:51/(CGFloat)255 blue:0 alpha:1.0];
[l1 setAdjustsFontSizeToFitWidth:YES];
[l1 setTextAlignment:UITextAlignmentRight];
l1.tag = L1_TAG;
[cell.contentView addSubview:l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 20 , 300, 30)];
[l2 setBackgroundColor:[UIColor clearColor]];
l2.font = [UIFont boldSystemFontOfSize:14];
l2.textColor = [UIColor blackColor];
//l2.textColor = [UIColor colorWithRed:102/(CGFloat)255 green:102/(CGFloat)255 blue:153/(CGFloat)255 alpha:1.0];
l2.tag = L2_TAG;
[l2 setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:l2];
UILabel* l3 = [[UILabel alloc] initWithFrame:CGRectMake(5, 40, 190, 30)];
[l3 setBackgroundColor:[UIColor clearColor]];
[l3 setFont:[UIFont systemFontOfSize:14]];
[l3 setTextColor:[UIColor grayColor]];
// l3.textColor = [UIColor colorWithRed:102/(CGFloat)255 green:102/(CGFloat)255 blue:0/(CGFloat)255 alpha:1.0];
[l3 setAdjustsFontSizeToFitWidth:YES];
l3.tag = L3_TAG;
[cell.contentView addSubview:l3];
UILabel* l4 = [[UILabel alloc] initWithFrame:CGRectMake(5, 60, 190, 30)];
[l4 setBackgroundColor:[UIColor clearColor]];
[l4 setFont:[UIFont systemFontOfSize:14]];
[l4 setTextColor:[UIColor grayColor]];
//l4.textColor = [UIColor colorWithRed:0/(CGFloat)255 green:51/(CGFloat)255 blue:0/(CGFloat)255 alpha:1.0];
[l4 setAdjustsFontSizeToFitWidth:YES];
l4.tag = L4_TAG;
[cell.contentView addSubview:l4];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(150, 60, 70, 30);
[button setTitle:#"Pay" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[button addTarget:self action:#selector(payButton)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
UILabel *myl1 = (UILabel*)[cell viewWithTag:L1_TAG];
UILabel *myl2 = (UILabel*)[cell viewWithTag:L2_TAG];
UILabel *myl3 = (UILabel*)[cell viewWithTag:L3_TAG];
UILabel *myl4 = (UILabel*)[cell viewWithTag:L4_TAG];
NSDictionary *dictionary = [vendorsArray objectAtIndex:indexPath.row];
myl1.text= [NSString stringWithFormat:#"INo:%#",[dictionary objectForKey:#"iNo"]];
myl2.text=[dictionary objectForKey:#"vendorName"];
myl3.text= [NSString stringWithFormat:#"Amount:%#",[dictionary objectForKey:#"amount"]];
myl4.text= [NSString stringWithFormat:#"DDate:%#",[dictionary objectForKey:#"dDate"]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
NSLog(#"count is %d",[vendorsArray count]);
return [vendorsArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Payables";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
-(IBAction) payButton{
PayDescriptionController *mPayDescriptionController = [[PayDescriptionController alloc]initWithNibName:#"PayDescriptionController" bundle:nil];
mPayDescriptionController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:mPayDescriptionController animated:YES];
}
Where i am going wrong in the above code.
try this,
[self.navigationController pushViewController:mPayDescriptionController animated:YES];
see ,it is navigating to next view or not !! and if not then may be one of your object is null.