How to have multiple line in one cell without customising? - ios

When I use NSLog it come in two lines but when I use it in the cell everything after \n will be vanished. I google this and there is wrap function which is deprecated.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
NSString *eventName = [object objectForKey:#"name"];
eventName = [eventName stringByAppendingString:dateString];
//eventName = [NSString stringWithFormat: #"%#\\r\\n", eventName];
eventName = [eventName stringByAppendingString:#"\n"];
NSLog(#"%#",eventName);
cell.textLabel.text = eventName;
return cell;
}
Output:
ddd
03:00PM, 30 Aug - 03:00PM, 01 Sep
Here is what I have:

Use like this
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
Here 0 represents infinite number of lines for a label. If you just want only 2 lines replace 0 with 2.

Related

Inserting a new cell in between the cells in tableview

i here attached a snapshot of my sample output.now it has two cells that is according to arraycount which i used at number of rows in section method.i have calutalted the break time ,now i want to insert one more cells in between that two cells to dispay the break time . here is my code whic i did ....please help me to do this .thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"%#",self.arrData1);
NSDictionary *tempDictionary= [self.arrData1 objectAtIndex:indexPath.row];
//NSLog(#"%#",tempDictionary);
NSString *strFunctionCodeValue =[tempDictionary objectForKey:#"function_code"];
NSString *strStartTimeValue = [tempDictionary objectForKey:#"start_time"];
NSString *strEndTimeValue = [tempDictionary objectForKey:#"end_time"];
NSString *strTotalUnitsValue =[tempDictionary objectForKey:#"total_units"];
NSString *strTotalTimeValue = [tempDictionary objectForKey:#"total_time"];
//NSString *strBreakTimeValue = [tempDictionary objectForKey:#"break_time"];
cell.lblFunctionCodeValue.text = strFunctionCodeValue;
cell.lblStartTimeValue.text = strStartTimeValue;
cell.lblEndTimeValue.text = strEndTimeValue;
cell.lblTotalUnitsValue.text =strTotalUnitsValue;
cell.lblTotalTimeValue.text = strTotalTimeValue;
//cell.lblBreakTimeValue.text = strBreakTimeValue;
return cell;
}

How can I set IndentationLevel in UITableViewCell?

I am new to swift.
How can I set IndentationLevel in UITableViewCell?
I have already achieved through Objective C, but I am not able to achieve through Swift.
EDIT:
- (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] ;
}
cell.textLabel.text=[[self.menuTableAr objectAtIndex:indexPath.row] valueForKey:#"name"];
[cell setIndentationLevel:[[[self.menuTableAr objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}
Thanks
Assume that valueForKey will return a NSNumber Object
Try
let level = self.menuTableAr.objectAtIndex(indexPath.row).valueForKey("level") as! NSNumber
cell.indentationLevel = Int(level.intValue)

how to display number from 1 to array count in IOS 7?

how to display a number from 1 to array count in a table row .
am using following code.
- (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];
// NSInteger WeekList=[WeekArry count];
}
NSInteger row = indexPath.row;
cell.textLabel.text=[NSString stringWithFormat:#"Week %ld",(long)row];
NSLog(#"row%ld",(long)indexPath.row);
// Configure the cell...
return cell;
}
if (row == array.count) {
cell.textLabel.text=[NSString stringWithFormat:#"Week %ld",(long)row];
}else{
cell.textLabel.text=[NSString stringWithFormat:#"Week %ld",(long)row + 1];
}

UITableViewCell.accessoryView will disappear in iOS 7

I assign some custom view to UITableViewCell.accessoryView, but if I scroll the tableView crazy, some accessoryView will disappear in iOS 7, and if I touch the cell, it's accessoryView can appear, I don't know why, because it's correct in iOS 6. This is my code, someone can help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView = [dict objectForKey:#"switch"];
}
else
{
cell.accessoryView = nil;
}
return cell;
}
When we use ReusableCellWithIdentifier in table view it reuse cells in table, you set cell.accessoryView = nil; it's remove accessory view for all cells in table view with same CellIdentifier try this code, it's solve your problem :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSDictionary * dict = [_dataSource objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryView = [dict objectForKey:#"switch"];
}
cell.textLabel.text = [NSString stringWithFormat:#"CELL %d", (int)indexPath.row+1];
if ([dict objectForKey:#"switch"])
{
cell.accessoryView.hidden=NO;
}
else
{
cell.accessoryView.hidden=YES;
}
return cell;
}

Adding Cells to UITableView from array

I pass an array called userArray which stores another array with strings. This is what I have so far but I know it's wrong. Can someone point me in the right direction?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
//Set Up Cell
DataSingleton *sharedData = [DataSingleton sharedData];
for (NSArray *array in sharedData.usersArray){
cell.primaryLabel.text = [array objectAtIndex:1];
cell.secondaryLabel.text = [array objectAtIndex:2];
cell.profileImage = [UIImage imageNamed:#"111-user.png"];
return cell;
}
}
cellForRowAtIndexPath is a UITableViewDataSource method, and it asks the data for single cell only. So, you have to remove a loop, and set up a single cell at once, using indexPath.row as an array index in your DataSingleton
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
//Set Up Cell
DataSingleton *sharedData = [DataSingleton sharedData];
NSArray *array = [sharedData.usersArray objectAtIndex:indexPath.row];
cell.primaryLabel.text = [array objectAtIndex:1];
cell.secondaryLabel.text = [array objectAtIndex:2];
cell.profileImage = [UIImage imageNamed:#"111-user.png"];
return cell;
}
Also, you should implement tableView:numberOfRowsInSection: to return [[[DataSingleton sharedData] usersArray] count]

Resources