Table cell with multiple actions - iphone

How do I accomplish something like this:
a table cell with multiple actions inside the text(link to image, since I don't have enough point to paste images)
I want to be able to control what the bold text does (e.g. activate a segue) and I also want the cell itself to have a separate action.
Is this possible? The text is dynamic, so the string lengths are not fixed.
Any help is much appreciated! (this is my first question of StackOverflow btw).

Have a look at TTTAttributedLabel, it can handle multiple links inside text.

Check the below 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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *indexpath_str=[NSString stringWithFormat:#"%d",indexPath.row+1];
NSString *ordinary_string=[NSString stringWithFormat:#"This is %d",indexPath.row+1];
NSMutableAttributedString *attri_str=[[NSMutableAttributedString alloc]initWithString:ordinary_string];
int last=[indexpath_str length];
int begin=[ordinary_string length]-[indexpath_str length];
[attri_str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(begin,last)];
[attri_str addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"HelveticaNeue-Bold" size:30] range:NSMakeRange(begin, last)];
cell.textLabel.attributedText =attri_str;
return cell;
}

Related

xcode - How to load images from xml the right way?

I'm quite new to xcode na objective-c so I'm sorry for the dumb question. What I want to do is to load images to cells in UITableView according to id. Every object in xml has the id and the image name id id.jpg. I accomplished to load them right but when I scroll the images dissapear. From what I have found it looks like it has something to do with dequeueReusableCellWithIdentifier but I can't figure out what is it. Here is the code I'm using to load the images:
-(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];
}
theList = [app.listArray objectAtIndex:indexPath.row];
NSString *trimmedString = [theList.t_image stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.imageView.image = [UIImage imageNamed:trimmedString];
cell.textLabel.text = theList.t_name;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if(cell.imageView.image == nil){
cell.imageView.image = [UIImage imageNamed:#"untitled.jpg"];
}
return cell;
}
and here is the xml:
<taxis>
<taxi>
<t_id>1</t_id>
<t_image>1.jpg</t_image>
<t_name>AAA Taxi</t_name>
<t_tel>+42014014</t_tel>
<t_addr>Vodičkova 40</t_addr>
<t_web>www.radiotaxiaaa.cz</t_web>
<t_getin>40</t_getin>
<t_km>26.9</t_km>
<t_wait>5</t_wait>
<t_city>Praha</t_city>
</taxi>
<taxi>
<t_id>2</t_id>
<t_image>2.jpg</t_image>
<t_name>Modrý anděl</t_name>
<t_tel>+420737222333</t_tel>
<t_addr>Cukrovarská 33</t_addr>
<t_web>www.modryandel.cz</t_web>
<t_getin>40</t_getin>
<t_km>19</t_km>
<t_wait>6</t_wait>
<t_city>Praha</t_city>
</taxi>
</taxis>
Thanks for any answer.
do all rows have picture? if not make sure to set cell.imageView to nil at the beginnign of the method after
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UPDATE : If i were you i would cache the images in an array and load them from this array in cellforrowatindexpath method.Try this i will probably work.Note that you can not add nil as object to NSmutablearray so you need to add another value rather than nil and UIimage

how to show two different values in one row of UITable?

I have one NSMutableArray having two strings in it
how I want my result in a row of table with some space as like below
Hotel Royal ___ First
Hotel Taj _____ Second
here _ ----> spaces
if you can't understand my question, you can ask me again regarding it
Thanks in Advance, I do upvote and appreciate proper answer...
either you use a custom cell or you use plain UITableViewCells.
This is the code you could use for a standard UITableViewCell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
//cell.textLabel.text = [array1 objectAtIndex:indexpath.row]; // Hotel Royal
//cell.detailTextLabel.text = [array2 objectAtIndex:indexpath.row]; // First
cell.textLabel.text = [array objectAtIndex:0]; // Hotel Royal
cell.detailTextLabel.text = [array objectAtIndex:1]; // First
return cell;
}
instead of UITableViewCellStyleValue1 you can test if one of the other styles suits your needs. UITableViewCellStyleSubtitle or UITableViewCellStyleValue2
You have to add two different UILables to the contentview of the cell if you want the text to appear on same line.
Check this great tutorial for code samples: Easy custom UITableView drawing
Chapter Layout within the contentView answers your question.

UITableView section details drawing wrong on scroll

I have a UITableView, and I am showng data based on indexPath.section, however, when I scroll my table view very quickly, its data keep overlapping. How to fix 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];
}
switch(indexPath.section)
{
// case 0 to 10;
//values change if I scroll my table
}
}
I think you are allocating some labels inside the cellForRowAtIndex delegate method. Thats why you got this problem. You can solve this in 2 ways:
Alloc the labels outside the delegate
method. set tags for your labels and
use it inside the cellForRowAtIndex
by refering the tags.
use custom cell view controller.
i fixed the issues
here is the solution if any one need
NSString *CellIdentifier=nil ;
NSMutableArray *Array= [[[NSMutableArray alloc] initWithObjects: #"One",#"Two", #"Three",#"Ad",#"Ae",#"Ah",#"Aj" ,nil]autorelease];
CellIdentifier = [Array objectAtIndex:indexPath.section];
///302-1021-9244-4658-1994-3384
UITableViewCell * cell = [tabelView dequeueReusableCellWithIdentifier:CellIdentifier];
Thanks

UITableViewCell showing indexpath rows out of order

Odd question, I am creating a UITableView, setting 8 rows of data. Simply, I'm just returning the indexpath row to try and figure out what is wrong, but ultimately, any rows that are off screen seem to load in a strange order.
For example, rows 0,1,2,3,4,5 all appear ok on first load, but then 6 & 7 are off screen. When I scroll to them I usually see rows 6 and 0, or 6 and 1. I then see row 7 appear back at the top of the table when I scroll back up.
Here's my 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] autorelease];
[cell.textLabel setText:[NSString stringWithFormat:#"Row: %d", [indexPath row]]];
}
return cell;
}
Am I doing something really stupid? Cheers
You can use
NSString *cellIdentifier =[NSString stringWithFormat:#"%d",indexPath.row];
instead of
static NSString *CellIdentifier = #"Cell";
Create your cell in the if statement and then set the properties (ie the label) outside the if statement.

iphone - when exactly does cellForRowAtIndexPath for table views get called and by what in

I just have a basic (noob) question about cellForRowAtIndexPath get called?
I'm working through example code and I don't see it explicitly called anywhere?
Does any component of type UITableViewComponent automatically call this function when it is created?
Thanks
When a UITableView is displaying this method gets called per row. In it you will customize each cell with particular data for display.
Here's the class reference.
Here's a sample:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"FriendCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
Item *i = [itemArray objectAtIndex:row];
cell.textLabel.text = [i name];
cell.detailTextLabel.text = [i brewery];
[i release];
return cell;
}
This is called for each cell when the corresponding UITableView gets drawn. An index path is passed in, and based on the section and cell numbers, your code should generate a UITableViewCell to be displayed in the UI.
You can look here for a quick tutorial into how UITableViews work.